Sign inGet started
← Back to all guides

How to analyze Google ads data in Python using Deepnote

By Filip Žitný

Updated on August 13, 2024

Analyzing Google Ads data can provide invaluable insights into the effectiveness of your marketing campaigns, helping you optimize performance and maximize ROI. Python, with its robust data manipulation libraries, is a popular choice for data scientists, data engineers, and data analysts. In this guide, we’ll walk you through the process of analyzing Google Ads data using Python in Deepnote, a collaborative data science platform.

Introduction to Google Ads data

Google Ads (formerly known as Google AdWords) is one of the most powerful advertising platforms, offering detailed metrics like impressions, clicks, conversions, cost, and more. Understanding and analyzing this data is crucial for optimizing ad performance.

Key metrics to analyze:

  • Impressions: how often your ad is shown.
  • Clicks: how often your ad is clicked.
  • CTR (click-through rate): clicks divided by impressions.
  • CPC (cost per click): cost divided by the number of clicks.
  • Conversions: actions taken after interacting with your ad.

Setting up Deepnote for Google ads analysis

Deepnote is a cloud-based data science notebook that integrates seamlessly with Python. It offers powerful collaboration features, making it ideal for team-based projects.

Steps to get started

  1. Sign up and create a project: if you don’t already have a Deepnote account, sign up for free and create a new project.

  2. Install required libraries: ensure you have the necessary Python libraries installed in your environment.

    !pip install google-ads pandas numpy matplotlib seaborn scikit-learn
  3. Set up API credentials: to access Google Ads data, you need to have access to the Google Ads API. Set up your credentials in Deepnote securely.

Fetching Google ads data

To fetch Google Ads data, you need to interact with the Google Ads API. Here’s how you can do it in Python

Authenticate and connect

from google.ads.google_ads.client import GoogleAdsClient
from google.ads.google_ads.errors import GoogleAdsException

# Load credentials from the google-ads.yaml file
client = GoogleAdsClient.load_from_storage('path_to_your_google_ads.yaml')

# Initialize the Google Ads Service
ga_service = client.get_service("GoogleAdsService", version="v12")

Query data

You can query specific data using the Google Ads Query Language (GAQL). Here’s an example query

query = """
    SELECT
      campaign.id,
      campaign.name,
      ad_group.id,
      ad_group.name,
      ad_group_criterion.criterion_id,
      ad_group_criterion.keyword.text,
      ad_group_criterion.keyword.match_type,
      metrics.impressions,
      metrics.clicks,
      metrics.ctr,
      metrics.average_cpc,
      metrics.conversions
    FROM keyword_view
    WHERE segments.date DURING LAST_30_DAYS
    ORDER BY metrics.clicks DESC
    LIMIT 100
"""

response = ga_service.search(customer_id="YOUR_CUSTOMER_ID", query=query)

# Convert response to a pandas DataFrame
data = []
for row in response:
    data.append([
        row.campaign.id,
        row.campaign.name,
        row.ad_group.id,
        row.ad_group.name,
        row.ad_group_criterion.criterion_id,
        row.ad_group_criterion.keyword.text,
        row.ad_group_criterion.keyword.match_type,
        row.metrics.impressions,
        row.metrics.clicks,
        row.metrics.ctr,
        row.metrics.average_cpc,
        row.metrics.conversions
    ])

import pandas as pd
df = pd.DataFrame(data, columns=[
    'campaign_id', 'campaign_name', 'ad_group_id', 'ad_group_name',
    'criterion_id', 'keyword_text', 'match_type', 'impressions',
    'clicks', 'ctr', 'average_cpc', 'conversions'
])

Data cleaning and preprocessing

Before diving into analysis, ensure your data is clean and ready. Common preprocessing steps include

  • Handling missing values: Drop or impute missing data.
  • Data type conversion: Ensure all columns have the correct data types.
  • Outlier detection: Identify and handle outliers in metrics like clicks or conversions.
# Check for missing values
df.isnull().sum()

# Fill missing numerical values with 0
df.fillna(0, inplace=True)

# Convert data types if necessary
df['campaign_id'] = df['campaign_id'].astype(str)

Data exploration and analysis

With clean data, you can start exploring and analyzing it.

Basic descriptive statistics

# Summary statistics
df.describe()

# Grouping data by campaign to see overall performance
campaign_performance = df.groupby('campaign_name').agg({
    'impressions': 'sum',
    'clicks': 'sum',
    'conversions': 'sum',
    'average_cpc': 'mean'
}).reset_index()

Identify top-performing keywords

top_keywords = df.groupby('keyword_text').agg({
    'impressions': 'sum',
    'clicks': 'sum',
    'conversions': 'sum'
}).sort_values(by='conversions', ascending=False).head(10)

Advanced analytics with machine learning

Leverage machine learning to gain deeper insights or predict future performance. For example, you could use regression to predict conversions based on clicks and impressions.

Predicting conversions with linear regression

from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression

# Prepare data for modeling
X = df[['clicks', 'impressions', 'average_cpc']]
y = df['conversions']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# Initialize and train model
model = LinearRegression()
model.fit(X_train, y_train)

# Evaluate the model
print(f"Model R^2 Score: {model.score(X_test, y_test)}")

Visualizing Google ads data

Visualization helps in understanding data trends and patterns. Matplotlib and Seaborn are excellent choices for creating insightful visualizations.

Visualizing clicks vs conversions

import matplotlib.pyplot as plt
import seaborn as sns

# Scatter plot of Clicks vs Conversions
plt.figure(figsize=(10, 6))
sns.scatterplot(data=df, x='clicks', y='conversions')
plt.title('Clicks vs conversions')
plt.show()

Campaign performance bar chart

plt.figure(figsize=(12, 8))
sns.barplot(x='conversions', y='campaign_name', data=campaign_performance.sort_values('conversions', ascending=False))
plt.title('Campaign performance by conversions')
plt.show()

Conclusion

Analyzing Google Ads data in Python using Deepnote allows data scientists, data engineers, and data analysts to derive actionable insights that can significantly improve ad performance. By fetching data directly from the Google Ads API, cleaning and preprocessing it, performing exploratory and advanced analysis, and visualizing the results, you can make data-driven decisions to optimize your campaigns.

Whether you are looking to report on the effectiveness of your campaigns or use predictive analytics to forecast future performance, Python in Deepnote provides a powerful environment to achieve these goals.

Filip Žitný

Data Scientist

Follow Filip on Twitter, LinkedIn and GitHub

That’s it, time to try Deepnote

Get started – it’s free
Book a demo

Footer

Solutions

  • Notebook
  • Data apps
  • Machine learning
  • Data teams

Product

Company

Comparisons

Resources

  • Privacy
  • Terms

© Deepnote