How To Access Google Analytics API Via Python

Posted by

[]The Google Analytics API provides access to Google Analytics (GA) report information such as pageviews, sessions, traffic source, and bounce rate.

[]The official Google documentation explains that it can be used to:

  • Develop custom dashboards to show GA information.
  • Automate complex reporting tasks.
  • Integrate with other applications.

[]You can access the API response utilizing a number of different approaches, consisting of Java, PHP, and JavaScript, however this short article, in specific, will concentrate on accessing and exporting information utilizing Python.

[]This short article will just cover some of the approaches that can be utilized to gain access to different subsets of data using various metrics and dimensions.

[]I intend to write a follow-up guide exploring various methods you can evaluate, visualize, and combine the information.

Establishing The API

Producing A Google Service Account

[]The first step is to develop a task or select one within your Google Service Account.

[]As soon as this has been created, the next step is to pick the + Produce Service Account button.

Screenshot from Google Cloud, December 2022 You will then be promoted to include some details such as a name, ID, and description.< img src= "//"alt="Service Account Details"width="1152"height=" 1124"data-src=""/ > Screenshot from Google Cloud, December 2022 Once the service account has actually been produced, browse to the secret area and include a new key. Screenshot from Google Cloud, December 2022 [] This will prompt you to produce and download a personal secret. In this instance, select JSON, and then create and

wait on the file to download. Screenshot from Google Cloud, December 2022

Add To Google Analytics Account

[]You will also wish to take a copy of the e-mail that has actually been created for the service account– this can be discovered on the primary account page.

Screenshot from Google Cloud, December 2022 The next action is to include that email []as a user in Google Analytics with Analyst approvals. Screenshot from Google Analytics, December 2022

Allowing The API The final and arguably crucial step is guaranteeing you have actually enabled access to the API. To do this, guarantee you remain in the right task and follow this link to allow gain access to.

[]Then, follow the actions to enable it when promoted.

Screenshot from Google Cloud, December 2022 This is required in order to access the API. If you miss this action, you will be prompted to finish it when first running the script. Accessing The Google Analytics API With Python Now everything is set up in our service account, we can start writing the []script to export the data. I chose Jupyter Notebooks to develop this, but you can likewise utilize other incorporated designer

[]environments(IDEs)consisting of PyCharm or VSCode. Installing Libraries The initial step is to install the libraries that are needed to run the rest of the code.

Some are special to the analytics API, and others are useful for future sections of the code.! pip set up– upgrade google-api-python-client! pip3 set up– upgrade oauth2client from apiclient.discovery import build from oauth2client.service _ account import ServiceAccountCredentials! pip install link! pip install functions import connect Note: When using pip in a Jupyter note pad, add the!– if running in the command line or another IDE, the! isn’t required. Producing A Service Construct The next action is to set []up our scope, which is the read-only analytics API authentication link. This is followed by the client tricks JSON download that was created when producing the personal key. This

[]is utilized in a comparable way to an API secret. To quickly access this file within your code, ensure you

[]have conserved the JSON file in the very same folder as the code file. This can then easily be called with the KEY_FILE_LOCATION function.

[]Finally, add the view ID from the analytics account with which you wish to access the data. Screenshot from author, December 2022 Altogether

[]this will appear like the following. We will reference these functions throughout our code.

SCOPES = [‘’] KEY_FILE_LOCATION=’client_secrets. json’ VIEW_ID=’XXXXX’ []Once we have actually included our private key file, we can add this to the credentials operate by calling the file and setting it up through the ServiceAccountCredentials action.

[]Then, set up the develop report, calling the analytics reporting API V4, and our already defined credentials from above.

qualifications = ServiceAccountCredentials.from _ json_keyfile_name(KEY_FILE_LOCATION, SCOPES) service = construct(‘analyticsreporting’, ‘v4’, credentials=qualifications)

Writing The Demand Body

[]Once we have whatever set up and defined, the real fun starts.

[]From the API service build, there is the ability to select the components from the response that we wish to access. This is called a ReportRequest item and needs the following as a minimum:

  • A valid view ID for the viewId field.
  • A minimum of one legitimate entry in the dateRanges field.
  • At least one valid entry in the metrics field.

[]View ID

[]As mentioned, there are a few things that are required during this develop phase, starting with our viewId. As we have already defined formerly, we simply require to call that function name (VIEW_ID) rather than including the entire view ID again.

[]If you wanted to gather data from a various analytics see in the future, you would simply need to alter the ID in the initial code block instead of both.

[]Date Range

[]Then we can include the date range for the dates that we want to gather the information for. This includes a start date and an end date.

[]There are a number of methods to compose this within the construct demand.

[]You can pick defined dates, for instance, in 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 see information from the last thirty days, you can set the start date as ’30daysAgo’ and completion date as ‘today.’

[]Metrics And Dimensions

[]The last action of the fundamental action call is setting the metrics and measurements. Metrics are the quantitative measurements from Google Analytics, such as session count, session period, and bounce rate.

[]Dimensions are the qualities of users, their sessions, and their actions. For instance, page path, traffic source, and keywords used.

[]There are a great deal of different metrics and dimensions that can be accessed. I won’t go through all of them in this post, but they can all be found together with additional info and associates here.

[]Anything you can access in Google Analytics you can access in the API. This includes goal conversions, starts and values, the internet browser gadget used to access the site, landing page, second-page course tracking, and internal search, site speed, and audience metrics.

[]Both the metrics and measurements are added in a dictionary format, utilizing secret: worth pairs. For metrics, the secret will be ‘expression’ followed by the colon (:-RRB- and after that the worth of our metric, which will have a particular format.

[]For example, if we wished to get a count of all sessions, we would add ‘expression’: ‘ga: sessions’. Or ‘expression’: ‘ga: newUsers’ if we wished to see a count of all brand-new users.

[]With dimensions, the secret will be ‘name’ followed by the colon once again and the value of the measurement. For example, if we wanted to draw out the various page courses, it would be ‘name’: ‘ga: pagePath’.

[]Or ‘name’: ‘ga: medium’ to see the various traffic source recommendations to the website.

[]Integrating Dimensions And Metrics

[]The real value is in integrating metrics and dimensions to extract the key insights we are most thinking about.

[]For example, to see a count of all sessions that have been produced from various traffic sources, we can set our metric to be ga: sessions and our dimension to be ga: medium.

action = service.reports(). batchGet( body= ). perform()

Creating A DataFrame

[]The response we get from the API is in the kind of a dictionary, with all of the information in secret: value sets. To make the information easier to see and examine, we can turn it into a Pandas dataframe.

[]To turn our response into a dataframe, we first require to develop some empty lists, to hold the metrics and dimensions.

[]Then, calling the action output, we will add the data from the dimensions into the empty dimensions list and a count of the metrics into the metrics list.

[]This will extract the information and add it to our formerly empty lists.

dim = [] metric = [] for report in response.get(‘reports’, []: columnHeader = report.get(‘columnHeader’, ) dimensionHeaders = columnHeader.get(‘measurements’, [] metricHeaders = columnHeader.get(‘metricHeader’, ). get(‘metricHeaderEntries’, [] rows = report.get(‘information’, ). get(‘rows’, [] for row in rows: dimensions = row.get(‘measurements’, [] dateRangeValues = row.get(‘metrics’, [] for header, measurement 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(value)) []Adding The Response Data

[]Once the information is in those lists, we can easily turn them into a dataframe by defining the column names, in square brackets, and appointing the list values to each column.

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

< img src= "" alt="DataFrame Example"/ > More Action Demand Examples Multiple Metrics There is likewise the capability to integrate several metrics, with each set included curly brackets and separated by a comma. ‘metrics’: [, ] Filtering []You can likewise request the API reaction just returns metrics that return specific requirements by including metric filters. It utilizes the following format:

if operator comparisonValue return the metric []For instance, if you just wished to extract pageviews with more than 10 views.

reaction = service.reports(). batchGet( body= ‘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”]]] ). perform() []Filters likewise work for dimensions in a similar way, however the filter expressions will be a little various due to the particular nature of measurements.

[]For instance, if you only wish to extract pageviews from users who have gone to the site using the Chrome browser, you can set an EXTRACT operator and use ‘Chrome’ as the expression.

reaction = service.reports(). batchGet( body= ‘reportRequests’: [‘viewId’: VIEW_ID, ‘dateRanges’: [‘startDate’: ’30daysAgo’, ‘endDate’: ‘today’], ‘metrics’: [‘expression’: ‘ga: pageviews’], “dimensions”: [“name”: “ga: internet browser”], “dimensionFilterClauses”: []] ). execute()


[]As metrics are quantitative measures, there is also the ability to write expressions, which work likewise to calculated metrics.

[]This involves specifying an alias to represent the expression and finishing a mathematical function on 2 metrics.

[]For example, you can compute completions per user by dividing the number of completions by the number of users.

reaction = service.reports(). batchGet( body= ‘reportRequests’: [‘viewId’: VIEW_ID, ‘dateRanges’: [], “metrics”: [ga: users”, “alias”: “completions per user”]] ). perform()


[]The API also lets you container dimensions with an integer (numerical) worth into varieties using histogram pails.

[]For example, bucketing the sessions count dimension into four buckets of 1-9, 10-99, 100-199, and 200-399, you can use the HISTOGRAM_BUCKET order type and specify the varieties in histogramBuckets.

action = service.reports(). batchGet( body= ). perform() Screenshot from author, December 2022 In Conclusion I hope this has supplied you with a basic guide to accessing the Google Analytics API, composing some various demands, and collecting some meaningful insights in an easy-to-view format. I have actually added the construct and request code, and the snippets shared to this GitHub file. I will like to hear if you attempt any of these and your prepare for exploring []the information even more. More resources: Featured Image: BestForBest/SMM Panel