Build a Custom Python SEO Dashboard: Automate Your SEO Analytics with Google Analytics and Search Console

Welcome to the World of Python-Driven SEO Magic! 🐍✨

Let’s face it—keeping track of SEO metrics manually from different tools like Google Analytics and Google Search Console is like trying to juggle flaming swords. Sure, you might pull it off once in a while, but it’s a recipe for disaster (and a whole lot of wasted time).

That’s where Python comes in to save the day (or at least your sanity). In this tutorial, we’ll walk you through the process of creating a custom SEO dashboard that pulls real-time data from Google Analytics, Google Search Console, and other sources—automatically! Whether you’re a newbie or a seasoned SEO expert, this dashboard will save you countless hours and give you fresh insights into your website’s performance, all in one place. Plus, we’ll add some fun visuals to spice things up!

So, grab your coding hat (and maybe a cup of coffee), because we’re about to dive into some serious Python-powered SEO magic. Let’s get started!


What You’ll Need

Before diving into the code, here’s a list of tools and libraries you’ll need to get started:

1. Python

  • You’ll need to have Python 3.x installed on your machine. If you haven’t already installed it, you can get it from Python’s official website.

2. Google Analytics API and Google Search Console API

3. Python Libraries

  • We’ll use the following Python libraries to make the process smoother:
    • google-api-python-client – To interact with Google Analytics and Google Search Console APIs.
    • pandas – For data manipulation and storage.
    • matplotlib and seaborn – For data visualization.
    • oauth2client – For authenticating Google API access.
    • requests – For API requests.
    • flask (optional) – For creating a simple web interface if you want to display the dashboard on a local server.

You can install the necessary libraries using pip:

pip install google-api-python-client pandas matplotlib seaborn oauth2client requests flask

Step 1: Set Up API Access

Google Analytics API Access

  1. Go to the Google API Console.
  2. Create a new project and enable the Google Analytics API.
  3. Create OAuth 2.0 credentials (choose “Desktop App”) and download the credentials.json file.
  4. Install the required Python package and authenticate the API.

Google Search Console API Access

  1. Enable the Google Search Console API in your Google Cloud project.
  2. Create OAuth credentials, just like you did for Google Analytics, and download the credentials file.
  3. You’ll use the credentials file to authenticate access to the Search Console data.

Step 2: Authenticate and Pull Data from APIs

First, let’s authenticate and set up the connections to both Google Analytics and Google Search Console APIs.

Google Analytics Authentication

from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import os
import pickle

# Scopes for Google Analytics API
SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']

def authenticate_google_analytics():
    """Authenticate and return the Google Analytics API service."""
    creds = None
    # The file token.pickle stores the user's access and refresh tokens.
    # It is created automatically when the authorization flow completes for the first time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    # Build the API client
    service = build('analyticsreporting', 'v4', credentials=creds)
    return service

Google Search Console Authentication

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow

# Scopes for Google Search Console API
SCOPES = ['https://www.googleapis.com/auth/webmasters.readonly']

def authenticate_search_console():
    """Authenticate and return the Google Search Console API service."""
    flow = InstalledAppFlow.from_client_secrets_file(
        'credentials.json', SCOPES)
    creds = flow.run_local_server(port=0)
    service = build('searchconsole', 'v1', credentials=creds)
    return service

Step 3: Fetch Data from Google Analytics and Google Search Console

Google Analytics Data (Example: Page Views)

def get_ga_data(service, view_id):
    """Fetch page views data from Google Analytics."""
    response = service.reports().batchGet(
        body={
            'reportRequests': [
                {
                    'viewId': view_id,
                    'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'yesterday'}],
                    'metrics': [{'expression': 'ga:sessions'}],
                    'dimensions': [{'name': 'ga:pagePath'}],
                }]
        }).execute()

    return response

Google Search Console Data (Example: Search Queries)

def get_gsc_data(service, site_url):
    """Fetch search query data from Google Search Console."""
    response = service.searchanalytics().query(
        siteUrl=site_url,
        body={
            'startDate': '2023-01-01',
            'endDate': '2023-01-31',
            'dimensions': ['query'],
            'rowLimit': 10
        }).execute()

    return response

Step 4: Organize and Display the Data

Once you’ve pulled the data, you’ll likely want to store it in a DataFrame for easy manipulation and visualization. We’ll use Pandas for this.

import pandas as pd

def process_ga_data(ga_response):
    """Process the Google Analytics data into a pandas DataFrame."""
    data = []
    for report in ga_response.get('reports', []):
        for row in report.get('data', {}).get('rows', []):
            data.append(row['dimensions'] + row['metrics'][0]['values'])
    
    df = pd.DataFrame(data, columns=['Page', 'Sessions'])
    return df

def process_gsc_data(gsc_response):
    """Process the Google Search Console data into a pandas DataFrame."""
    data = []
    for row in gsc_response.get('rows', []):
        data.append([row['keys'][0], row['clicks'], row['impressions']])
    
    df = pd.DataFrame(data, columns=['Query', 'Clicks', 'Impressions'])
    return df

Step 5: Visualization

Using Matplotlib and Seaborn, you can create compelling visualizations of your SEO data.

import matplotlib.pyplot as plt
import seaborn as sns

def visualize_data(df):
    """Create a simple bar plot for page views and clicks."""
    plt.figure(figsize=(10, 6))
    sns.barplot(x='Page', y='Sessions', data=df)
    plt.title('Top Pages by Sessions')
    plt.xticks(rotation=45)
    plt.show()

Step 6: Putting It All Together

Now that you have data from both Google Analytics and Google Search Console, you can organize it into a dashboard. Here, we’ll use Flask to create a simple web interface to display the data.

from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def index():
    # Fetch and process data
    ga_service = authenticate_google_analytics()
    gsc_service = authenticate_search_console()

    ga_data = get_ga_data(ga_service, 'your-view-id')
    gsc_data = get_gsc_data(gsc_service, 'https://your-website.com')

    ga_df = process_ga_data(ga_data)
    gsc_df = process_gsc_data(gsc_data)

    return render_template('dashboard.html', ga_data=ga_df.to_html(), gsc_data=gsc_df.to_html())

if __name__ == '__main__':
    app.run(debug=True)

Conclusion

By following these steps, you can build a custom SEO dashboard that pulls in real-time data from Google Analytics, Google Search Console, and other sources. This dashboard will help you track key SEO metrics, visualize your website’s performance, and make data-driven decisions to improve your rankings.

If you found this tutorial helpful, let me know in the comments below or reach out to me on Twitter. Happy coding!

Leave a Comment