Using Python to Aggregate Data from Multiple SEO Tools

Using Python to Aggregate Data from Multiple SEO Tools

As a digital marketer or SEO specialist, you’re likely familiar with the importance of tracking key performance indicators (KPIs) for your online presence. However, manually collecting data from multiple sources can be time-consuming and prone to errors. In this article, we’ll explore how to use Python to aggregate data from various SEO tools, making it easier to gain insights into your website’s performance.

Prerequisites

Before diving in, ensure you have:

  1. Python 3.x installed on your computer.
  2. Basic knowledge of Python programming, including understanding of variables, loops, and conditional statements.
  3. Access to the API keys for each SEO tool you want to integrate.

Choosing the Right Libraries

To fetch data from various SEO tools, we’ll use Python libraries that interact with their APIs:

  1. google-api-python-client: For accessing Google Search Console and Analytics data.
  2. aiohttp: A lightweight asynchronous HTTP client for making requests to API endpoints.

Install these libraries using pip:
bash
pip install google-api-python-client aiohttp

Step 1: Set Up Your Environment


Create a new Python script (e.g., seo_data_aggregator.py) and import the necessary libraries:

python
import os
from googleapiclient.discovery import build
import aiohttp

Step 2: Define Your API Keys

Store your API keys for each SEO tool in a secure location. For this example, we’ll use environment variables to store them:

python
GOOGLE_SEARCH_CONSOLE_API_KEY = os.getenv('GOOGLE_SEARCH_CONSOLE_API_KEY')
GOOGLE_ANALYTICS_API_KEY = os.getenv('GOOGLE_ANALYTICS_API_KEY')
SEMRUSH_API_KEY = os.getenv('SEMRUSH_API_KEY')

Replace the os.getenv() calls with your actual API keys.

Step 3: Create a Function to Fetch Data

Write a function that fetches data from each SEO tool using their respective APIs:

“`python
async def get_seo_data(api_key):
# Google Search Console
if api_key.startswith(‘GOOGLE_’):
google_service = build(‘searchconsole’, ‘v1’)
response = google_service.searchConsole().get(request_body={‘dateRange’: {‘startDate’: ‘2022-01-01’, ‘endDate’: ‘2022-12-31’}})
return await response

# Google Analytics
elif api_key.startswith('GOOGLE_'):
    ga_service = build('analyticsdata', 'v1beta')
    request_body = {
        'dateRanges': [{'startDate': '2022-01-01', 'endDate': '2022-12-31'}],
        'metrics': ['sessions', 'bounceRate'],
        'dimensions': ['date']
    }
    response = ga_service.datapoint().query(request_body=request_body)
    return await response

# SEMrush
elif api_key.startswith('SEMRUSH_'):
    semrush_service = aiohttp.ClientSession()
    request_url = f'https://api.semrush.com/v2/organizations/{api_key}/dashboard?startDate=2022-01-01&endDate=2022-12-31'
    response = await semrush_service.get(request_url)
    return await response.json()

# Handle unknown API key
else:
    print(f"Unknown API key: {api_key}")

“`

Step 4: Aggregate and Process the Data

Once you’ve fetched data from each SEO tool, aggregate and process it using Python’s built-in functions:

“`python

Fetch data from each SEO tool

google_data = await get_seo_data(GOOGLE_SEARCH_CONSOLE_API_KEY)
ga_data = await get_seo_data(GOOGLE_ANALYTICS_API_KEY)
semrush_data = await get_seo_data(SEMRUSH_API_KEY)

Aggregate and process the data (example: extract total sessions from Google Analytics)

total_sessions = ga_data.get(‘rows’)[0].get(‘metricValue’)
“`

Conclusion

In this article, we’ve demonstrated how to use Python to aggregate data from multiple SEO tools. By leveraging libraries like google-api-python-client and aiohttp, you can create a robust script that fetches data from various sources and processes it for insights. Remember to handle errors and exceptions properly to ensure your script runs smoothly. Happy aggregating!