The Google Analytics API supplies entry to Google Analytics (GA) report information equivalent to pageviews, periods, visitors supply, and bounce price.
The official Google documentation explains that it may be used to:
- Construct customized dashboards to show GA information.
- Automate complicated reporting duties.
- Combine with different functions.
This text will simply cowl among the strategies that can be utilized to entry totally different subsets of information utilizing totally different metrics and dimensions.
I hope to put in writing 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 challenge or choose one inside your Google Service Account.
As soon as this has been created, the following step is to pick out the + Create Service Account button.
You’ll then be promoted so as to add some particulars equivalent to a reputation, ID, and outline.
As soon as the service account has been created, navigate to the KEYS part and add a brand new key.
It will immediate you to create and obtain a personal key. On this occasion, choose JSON, after which create and watch for the file to obtain.
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.
The following step is so as to add that e-mail as a person in Google Analytics with Analyst permissions.
Enabling The API
The ultimate and arguably most necessary step is making certain you’ve got enabled entry to the API. To do that, guarantee you’re within the appropriate challenge and comply with this link to enable access.
Then, comply with the steps to allow it when promoted.
That is wanted with a view to entry the API. Should you miss this step, you may 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 are able to begin writing the script to export the info.
Putting in Libraries
Step one is to put in the libraries which might be 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 following step is to arrange our scope, which is the read-only analytics API authentication hyperlink.
That is adopted by the shopper secrets and techniques JSON obtain that was generated when creating the non-public key. That is utilized in the same solution to an API key.
To simply entry this file inside your code, guarantee you’ve got saved the JSON file in the identical folder because the code file. This may then simply be known as with the KEY_FILE_LOCATION operate.
Lastly, add the view ID from the analytics account with which you want to entry the info.
Altogether this can appear to be the next. We’ll 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 are able to add this to the credentials operate by calling the file and setting it up by means 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 true enjoyable begins.
From the API service construct, there’s the power to pick out the weather from the response that we need to entry. That is known as a ReportRequest object and requires the next at least:
- A sound view ID for the viewId area.
- At the very least one legitimate entry within the dateRanges area.
- At the very least one legitimate entry within the metrics area.
As talked about, there are some things which might be wanted throughout this construct stage, beginning with our viewId. As now we have already outlined beforehand, we simply have to name that operate identify (VIEW_ID) slightly than including the entire view ID once more.
Should you needed to gather information from a unique analytics view sooner or later, you’ll simply want to vary the ID within the preliminary code block slightly than each.
Then we are able to 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 put in writing this throughout the construct request.
You may 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 ‘in the present day.’
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, equivalent to session rely, session period, and bounce price.
Dimensions are the traits of customers, their periods, and their actions. For instance, web page path, visitors supply, and key phrases used.
There are plenty of totally different metrics and dimensions that may be accessed. I received’t undergo all of them on this article, however they’ll all be discovered along with extra info and attributes here.
Something you may entry in Google Analytics you may entry within the API. This consists of aim conversions, begins and values, the browser system used to entry the web site, touchdown web page, second-page path monitoring, and inside 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 can 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 ‘identify’ 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 might be ‘identify’: ‘ga:pagePath’.
Or ‘identify’: ‘ga:medium’ to see the totally different visitors supply referrals to the positioning.
Combining Dimensions And Metrics
The true worth is in combining metrics and dimensions to extract the important thing insights we’re most keen on.
For instance, to see a rely of all periods which were created from totally different visitors sources, we are able to set our metric to be ga:periods and our dimension to be ga:medium.
response = service.reviews().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 all the information in key:worth pairs. To make the info simpler to view and analyze, we are able to flip it right into a Pandas dataframe.
To show our response right into a dataframe, we first have to create some empty lists, to carry the metrics and dimensions.
Then, calling the response output, we’ll append the info from the size into the empty dimensions listing and a rely of the metrics into the metrics listing.
It will extract the info and add it to our beforehand empty lists.
dim =  metric =  for report in response.get('reviews', ): 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 are able to 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()
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" ]
You may as well 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, in case you solely needed to extract pageviews with greater than ten views.
response = service.reviews().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 the same approach, however the filter expressions might be barely totally different because of the attribute nature of dimensions.
For instance, in case you 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.reviews().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()
As metrics are quantitative measures, there’s additionally the power to put in writing expressions, which work equally to calculated metrics.
This includes defining an alias to characterize the expression and finishing a mathematical operate on two metrics.
For instance, you may calculate completions per person by dividing the variety of completions by the variety of customers.
response = service.reviews().batchGet( physique= 'reportRequests': [ 'viewId': VIEW_ID, 'dateRanges': ['startDate': '30daysAgo', 'endDate': 'today'], "metrics": [ "expression": "ga:goal1completions/ga:users", "alias": "completions per user" ] ] ).execute()
The API additionally permits 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 utilize the HISTOGRAM_BUCKET order sort and outline the ranges in histogramBuckets.
response = service.reviews().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()
I hope this has offered you with a fundamental information to accessing the Google Analytics API, writing some totally different requests, and accumulating 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 in case you attempt any of those and your plans for exploring the info additional.
Featured Picture: BestForBest/Shutterstock