How To Access Google Analytics API Via Python

[ad_1]

The Google Analytics API offers entry to Google Analytics (GA) report information akin to pageviews, periods, site visitors supply, and bounce price.

The official Google documentation explains that it may be used to:

  • Construct customized dashboards to show GA information.
  • Automate advanced reporting duties.
  • Combine with different functions.

You’ll be able to entry the API response utilizing a number of totally different strategies, together with Java, PHP, and JavaScript, however this text, specifically, will deal with accessing and exporting information utilizing Python.

This text will simply cowl a few of the strategies that can be utilized to entry totally different subsets of information utilizing totally different metrics and dimensions.

I hope to write down a follow-up information exploring other ways you may analyze, visualize, and mix the info.

Setting Up The API

Creating A Google Service Account

Step one is to create a mission or choose one inside your Google Service Account.

As soon as this has been created, the following step is to pick the + Create Service Account button.

Google Service AccountScreenshot from Google Cloud, December 2022

You’ll then be promoted so as to add some particulars akin to a reputation, ID, and outline.

Service Account DetailsScreenshot from Google Cloud, December 2022

As soon as the service account has been created, navigate to the KEYS part and add a brand new key.

Service Account KeyScreenshot from Google Cloud, December 2022

This can immediate you to create and obtain a personal key. On this occasion, choose JSON, after which create and watch for the file to obtain.

JSON Credentials KeyScreenshot from Google Cloud, December 2022

Add To Google Analytics Account

Additionally, you will need to take a replica of the e-mail that has been generated for the service account – this may be discovered on the primary account web page.

Google Account EmailScreenshot from Google Cloud, December 2022

The subsequent step is so as to add that e mail as a person in Google Analytics with Analyst permissions.

Email in Google AnalyticsScreenshot from Google Analytics, December 2022

Enabling The API

The ultimate and arguably most necessary step is making certain you’ve gotten enabled entry to the API. To do that, guarantee you might be within the right mission and observe this hyperlink to allow entry.

Then, observe the steps to allow it when promoted.

Enabling the APIScreenshot from Google Cloud, December 2022

That is wanted to be able to entry the API. For those who miss this step, you can be prompted to finish it when first operating the script.

Accessing The Google Analytics API With Python

Now the whole lot is about up in our service account, we will begin writing the script to export the info.

I selected Jupyter Notebooks to create this, however it’s also possible to use different built-in developer environments (IDEs) together with PyCharm or VSCode.

Putting in Libraries

Step one is to put in the libraries which are wanted to run the remainder of the code.

Some are distinctive to the analytics API, and others are helpful for future sections of the code.

!pip set up --upgrade google-api-python-client
!pip3 set up --upgrade oauth2client
from apiclient.discovery import construct
from oauth2client.service_account import ServiceAccountCredentials
!pip set up join
!pip set up features
import join

Be aware: When utilizing pip in a Jupyter pocket book, add the ! – if operating within the command line or one other IDE, the ! isn’t wanted.

Creating A Service Construct

The subsequent step is to arrange our scope, which is the read-only analytics API authentication hyperlink.

That is adopted by the consumer secrets and techniques JSON obtain that was generated when creating the non-public key. That is utilized in an analogous strategy to an API key.

To simply entry this file inside your code, guarantee you’ve gotten saved the JSON file in the identical folder because the code file. This may then simply be referred to as with the KEY_FILE_LOCATION perform.

Lastly, add the view ID from the analytics account with which you wish to entry the info.

Google Analytics View IDScreenshot from creator, December 2022

Altogether it will seem like the next. We are going to reference these features all through our code.

SCOPES = ['https://www.googleapis.com/auth/analytics.readonly']
KEY_FILE_LOCATION = 'client_secrets.json'
VIEW_ID = 'XXXXX'

As soon as now we have added our non-public key file, we will add this to the credentials perform by calling the file and setting it up by way of the ServiceAccountCredentials step.

Then, arrange the construct report, calling the analytics reporting API V4, and our already outlined credentials from above.

credentials = ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, SCOPES)
service = construct('analyticsreporting', 'v4', credentials=credentials)

Writing The Request Physique

As soon as now we have the whole lot arrange and outlined, the actual enjoyable begins.

From the API service construct, there’s the power to pick the weather from the response that we need to entry. That is referred to as a ReportRequest object and requires the next at the least:

  • A sound view ID for the viewId area.
  • At the least one legitimate entry within the dateRanges area.
  • At the least one legitimate entry within the metrics area.

View ID

As talked about, there are some things which are wanted throughout this construct stage, beginning with our viewId. As now we have already outlined beforehand, we simply must name that perform title (VIEW_ID) somewhat than including the entire view ID once more.

For those who needed to gather information from a distinct analytics view sooner or later, you’d simply want to alter the ID within the preliminary code block somewhat than each.

Date Vary

Then we will add the date vary for the dates that we need to gather the info for. This consists of a begin date and an finish date.

There are a few methods to write down this throughout the construct request.

You’ll be able to choose outlined dates, for instance, between two dates, by including the date in a year-month-date format, ‘startDate’: ‘2022-10-27’, ‘endDate’: ‘2022-11-27’.

Or, if you wish to view information from the final 30 days, you may set the beginning date as ‘30daysAgo’ and the top date as ‘immediately.’

Metrics And Dimensions

The ultimate step of the essential response name is setting the metrics and dimensions. Metrics are the quantitative measurements from Google Analytics, akin to session rely, session length, and bounce price.

Dimensions are the traits of customers, their periods, and their actions. For instance, web page path, site visitors supply, and key phrases used.

There are plenty of totally different metrics and dimensions that may be accessed. I gained’t undergo all of them on this article, however they will all be discovered along with extra data and attributes right here.

Something you may entry in Google Analytics you may entry within the API. This consists of purpose conversions, begins and values, the browser system used to entry the web site, touchdown web page, second-page path monitoring, and inner search, website velocity, and viewers metrics.

Each the metrics and dimensions are added in a dictionary format, utilizing key:worth pairs. For metrics, the important thing might be ‘expression’ adopted by the colon (:) after which the worth of our metric, which may have a particular format.

For instance, if we needed to get a rely of all periods, we’d add ‘expression’: ‘ga:periods’. Or ‘expression’: ‘ga:newUsers’ if we needed to see a rely of all new customers.

With dimensions, the important thing might be ‘title’ adopted by the colon once more and the worth of the dimension. For instance, if we needed to extract the totally different web page paths, it will be ‘title’: ‘ga:pagePath’.

Or ‘title’: ‘ga:medium’ to see the totally different site visitors supply referrals to the positioning.

Combining Dimensions And Metrics

The actual worth is in combining metrics and dimensions to extract the important thing insights we’re most enthusiastic about.

For instance, to see a rely of all periods which were created from totally different site visitors sources, we will set our metric to be ga:periods and our dimension to be ga:medium.

response = service.studies().batchGet(
    physique={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'today'}],
          'metrics': [{'expression': 'ga:sessions'}],
          'dimensions': [{'name': 'ga:medium'}]
        }]
      }
  ).execute()

Creating A DataFrame

The response we get from the API is within the type of a dictionary, with the entire information in key:worth pairs. To make the info simpler to view and analyze, we will flip it right into a Pandas dataframe.

To show our response right into a dataframe, we first must create some empty lists, to carry the metrics and dimensions.

Then, calling the response output, we are going to append the info from the size into the empty dimensions listing and a rely of the metrics into the metrics listing.

This can extract the info and add it to our beforehand empty lists.

dim = []
metric = []

for report in response.get('studies', []):

columnHeader = report.get('columnHeader', {})
dimensionHeaders = columnHeader.get('dimensions', [])
metricHeaders = columnHeader.get('metricHeader', {}).get('metricHeaderEntries', [])
rows = report.get('information', {}).get('rows', [])

for row in rows:

dimensions = row.get('dimensions', [])
dateRangeValues = row.get('metrics', [])

for header, dimension in zip(dimensionHeaders, dimensions):
dim.append(dimension)

for i, values in enumerate(dateRangeValues):
for metricHeader, worth in zip(metricHeaders, values.get('values')):
metric.append(int(worth))

Including The Response Information

As soon as the info is in these lists, we will simply flip them right into a dataframe by defining the column names, in sq. brackets, and assigning the listing values to every column.

df = pd.DataFrame()
df["Sessions"]= metric
df["Medium"]= dim
df= df[["Medium","Sessions"]]
df.head()

DataFrame Example 

Extra Response Request Examples

A number of Metrics

There’s additionally the power to mix a number of metrics, with every pair added in curly brackets and separated by a comma.

'metrics': [
              {"expression": "ga:pageviews"},
              {"expression": "ga:sessions"}
          ]

Filtering

You can even request the API response solely returns metrics that return sure standards by including metric filters. It makes use of the next format:

if {metricName} {operator} {comparisonValue}
   return the metric

For instance, for those who solely needed to extract pageviews with greater than ten views.

response = service.studies().batchGet(
    physique={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'today'}],
          'metrics': [{'expression': 'ga:pageviews'}],
          'dimensions': [{'name': 'ga:pagePath'}],

"metricFilterClauses": [{
"filters": [{
"metricName": "ga:pageviews",
"operator": "GREATER_THAN",
"comparisonValue": "10"
}]
}]
}]
}
).execute()

Filters additionally work for dimensions in an analogous manner, however the filter expressions might be barely totally different as a result of attribute nature of dimensions.

For instance, for those who solely need to extract pageviews from customers who’ve visited the positioning utilizing the Chrome browser, you may set an EXTRACT operator and use ‘Chrome’ because the expression.

response = service.studies().batchGet(
    physique={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'today'}],
          'metrics': [{'expression': 'ga:pageviews'}],
          "dimensions": [{"name": "ga:browser"}],
          "dimensionFilterClauses": [
        {
          "filters": [
            {
              "dimensionName": "ga:browser",
              "operator": "EXACT",
              "expressions": ["Chrome"]
            }
          ]
        }
      ]
    }
  ]
}
).execute()

Expressions

As metrics are quantitative measures, there’s additionally the power to write down expressions, which work equally to calculated metrics.

This includes defining an alias to signify the expression and finishing a mathematical perform on two metrics.

For instance, you may calculate completions per person by dividing the variety of completions by the variety of customers.

response = service.studies().batchGet(
    physique={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'today'}],
          "metrics":
      [
        {
          "expression": "ga:goal1completions/ga:users",
          "alias": "completions per user"
        }
      ]
    }
  ]
}
).execute()

Histograms

The API additionally enables you to bucket dimensions with an integer (numeric) worth into ranges utilizing histogram buckets.

For instance, bucketing the periods rely dimension into 4 buckets of 1-9, 10-99, 100-199, and 200-399, you should use the HISTOGRAM_BUCKET order kind and outline the ranges in histogramBuckets.

response = service.studies().batchGet(
    physique={
        'reportRequests': [
        {
          'viewId': VIEW_ID,
          'dateRanges': [{'startDate': '30daysAgo', 'endDate': 'today'}],
          "metrics": [{"expression": "ga:sessions"}],
          "dimensions": [
        {
              "name": "ga:sessionCount",
              "histogramBuckets": ["1","10","100","200","400"]
        }
      ],
         "orderBys": [
        {
              "fieldName": "ga:sessionCount",
              "orderType": "HISTOGRAM_BUCKET"
        }
      ]
    }
  ]
}
).execute()
Histogram Example Screenshot from creator, December 2022

In Conclusion

I hope this has supplied you with a primary information to accessing the Google Analytics API, writing some totally different requests, and amassing some significant insights in an easy-to-view format.

I’ve added the construct and request code, and the snippets shared to this GitHub file.

I’ll love to listen to for those who strive any of those and your plans for exploring the info additional.

Extra sources:


Featured Picture: BestForBest/Shutterstock



[ad_2]

Scroll to Top