How To Create a Bing Ads Python Pipeline with PyAirbyte

10 min read
April 24, 2024

Extracting and managing data from Bing Ads can present several challenges, including complex API interactions, data consistency issues, and the need for scalable solutions.

PyAirbyte, a Python client for Airbyte, offers a streamlined way to tackle these challenges. It simplifies the data extraction process, supports incremental data loading to efficiently handle large volumes, and provides compatibility with various caching backends. This guide explores how PyAirbyte can alleviate common data pipeline headaches and enhance your data integration efforts with Bing Ads.

Traditional Methods for Creating Bing Ads Data Pipelines

Traditional methods for constructing data pipelines, specifically for aggregating data from platforms like Bing Ads, rely heavily on custom Python scripts. These scripts are written to directly interact with Bing Ads' API, fetch the required advertising data, and process it as per business needs. The process entails authenticating to the Bing Ads API, making the right API calls, handling pagination, managing API rate limits, and parsing the received data, often in JSON format, into a usable structure for further analysis or storage.

Pain Points in Extracting Data from Bing Ads

  1. API Complexity and Changes: Bing Ads API, like many other APIs, is complex and subject to frequent changes. Keeping up with these changes requires regular updates to the custom scripts, making maintenance a continual challenge.
  2. Authentication and Security Concerns: Properly managing authentication, including handling refresh tokens for OAuth2, adds an additional layer of complexity. Ensuring secure storage and handling of these tokens within scripts is paramount but not straightforward.
  3. Rate Limiting and Data Volume: Dealing with API rate limits efficiently while trying to fetch large volumes of data can be challenging. Custom scripts need to be smart enough to handle retries and back-offs without human intervention.
  4. Data Consistency and Error Handling: Ensuring data consistency when errors occur during data extraction is difficult. Custom scripts must be able to detect errors, manage partial data loads, and recover from failures, requiring sophisticated error handling mechanisms.
  5. Parsing and Data Transformation: Data extracted from the API must be parsed from its original format (often JSON) and transformed to fit into the target datastore. This transformation process can be complex, especially when dealing with nested structures.

Impact of These Challenges on Data Pipeline Efficiency and Maintenance

  • Increased Development Time: Developers spend a significant amount of time writing, testing, and maintaining custom scripts due to the aforementioned challenges. This diverts resources from core business activities.
  • Reduced Reliability: With the complexity around error handling and data consistency, there's a higher risk of data pipelines breaking down, leading to unreliable data flows and potentially impacting business decisions.
  • Difficulty in Scaling: As business requirements change and grow, scaling custom scripts to accommodate more data or integrate additional services becomes cumbersome and inefficient.
  • Maintenance Overhead: Ongoing maintenance to accommodate API changes, manage authentication tokens securely, and ensure the scripts accommodate evolving business needs requires continuous effort and expertise.

Implementing a Python Data Pipeline for Bing Ads with PyAirbyte

1. Installing PyAirbyte

pip install airbyte

This command installs the PyAirbyte package, which is a Python client for Airbyte, an open-source data integration tool. The package allows you to programmatically interact with Airbyte's capabilities directly from your Python environment.

2. Importing PyAirbyte and Setting Up the Bing Ads Source Connector

import airbyte as ab

source = ab.get_source(
   source-bing-ads,
   install_if_missing=True,
   config=
{
 "auth_method": "oauth2.0",
 "tenant_id": "common",
 "client_id": "exampleClientId123",
 "client_secret": "",
 "refresh_token": "exampleRefreshToken123",
 "developer_token": "exampleDeveloperToken123",
 "account_names": [
   {
     "operator": "Contains",
     "name": "exampleAccountName"
   }
 ],
 "reports_start_date": "2023-01-01",
 "lookback_window": 30,
 "custom_reports": [
   {
     "name": "Account Performance",
     "reporting_object": "AccountPerformanceReportRequest",
     "report_columns": [
       "AccountId",
       "AccountName"
     ],
     "report_aggregation": "Daily"
   }
 ]
}
)

This script imports the PyAirbyte module and creates a source connector for Bing Ads with the necessary configurations. You replace the placeholders with your specific OAuth credentials, developer token, and set up custom reports as needed. The install_if_missing parameter ensures that if the Bing Ads connector isn't already present, PyAirbyte will install it.

3. Verifying Connector Configuration

source.check()

After configuring the connector, this command checks the provided settings against the Bing Ads API to ensure everything is configured correctly and the connection can be established.

4. Listing Available Streams

source.get_available_streams()

This line lists all the data streams available from Bing Ads through the configured connector. Streams represent different sets of data that can be extracted, such as campaign performance or click metrics.

5. Selecting Data Streams

source.select_all_streams()

Here, you're opting to select all available streams for data extraction. Alternatively, if only specific streams are needed, the select_streams() method would allow for more granular selection.

6. Reading Data into a Cache

cache = ab.get_default_cache()
result = source.read(cache=cache)

This snippet initializes a default local cache (powered by DuckDB) and reads the selected streams from Bing Ads into this cache. You can also use custom cache settings to direct the data into other databases like Postgres, Snowflake, or BigQuery.

7. Extracting Data into a DataFrame

df = cache["your_stream"].to_pandas()

Finally, you pick a specific stream from the cache and convert it into a pandas DataFrame for data manipulation or analysis. This step makes the extracted data readily available for analytical processes, visualization, or further data processing tasks within Python.

By following these steps, you create a streamlined, code-managed pipeline for Bing Ads data extraction, leveraging the robust capabilities of PyAirbyte for data integration directly within your Python development environment.

For keeping up with the latest PyAirbyte’s features, make sure to check our documentation. And if you’re eager to see more code examples with PyAirbyte, check out our Quickstarts library.

Why Using PyAirbyte for Bing Ads Data Pipelines

Ease of Installation and Configuration

Installing PyAirbyte is straightforward; it requires only a Python environment and can be installed using pip, making it accessible to anyone with basic Python knowledge. Setting up and configuring available source connectors for services like Bing Ads is a hassle-free process with PyAirbyte. It supports the installation of custom source connectors as well, accommodating unique data integration needs beyond the pre-configured options.

Efficient Data Stream Selection

PyAirbyte offers the ability to selectively choose which data streams to extract from Bing Ads. This selective approach is advantageous as it allows for conserving computing resources by only processing relevant data, leading to more efficient and streamlined data pipelines.

Flexible Caching Options

The platform supports multiple caching backends, including DuckDB, MotherDuck, Postgres, Snowflake, and BigQuery, offering a high degree of flexibility in how data is cached during the extraction process. DuckDB serves as the default caching option when no specific cache is defined, ensuring ease of use for users who may not require specialized caching configurations.

Incremental Data Reading

One of the key features of PyAirbyte is its ability to read data incrementally. This capability is crucial for efficiently handling large datasets, as it reduces the load on the data source and ensures that only new or updated data is extracted in subsequent runs, saving both time and computational resources.

Compatibility with Python Libraries

PyAirbyte’s compatibility with popular Python libraries, like Pandas for data manipulation or various SQL-based tools for data management, unlocks a vast array of possibilities for data transformation and analysis. This compatibility seamlessly integrates PyAirbyte into existing Python-based data analysis, orchestration, and AI frameworks, making it a powerful tool for data scientists and engineers.

Enabling AI Applications

Given its ability to efficiently and flexibly pull in data from sources like Bing Ads and integrate with Python’s rich ecosystem of data processing and AI tools, PyAirbyte is ideally positioned to facilitate AI and machine learning applications. Whether it's feeding refined data into machine learning models, performing data analysis for AI-driven insights, or automating data workflows, PyAirbyte acts as a bridge between raw data sources and sophisticated AI applications.

Conclusion

In wrapping up this guide, we've explored the significant advantages PyAirbyte offers for streamlining Bing Ads data pipelines. Through its easy installation, flexible data extraction, and compatibility with powerful Python libraries, PyAirbyte simplifies the process of integrating Bing Ads data into your analytical workflows. Its ability to handle data efficiently, enabling incremental reads and offering various caching options, makes it a robust solution for managing data pipelines.

Whether you're an engineer building scalable data systems or a data scientist seeking to enrich AI models with Bing Ads insights, PyAirbyte provides the tools necessary to transform data integration from a complex chore into a streamlined, manageable process.

Do you have any questions or feedback for us? You can keep in touch by joining our Slack channel! If you want to keep up to date with new PyAirbyte features, subscribe to our newsletter.

Enhancing Python with Airbyte connectors
Try PyAirbyte