How To Access Google Analytics API Via Python

Posted by

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

[]The main Google documentation discusses that it can be used to:

  • Build customized dashboards to show GA information.
  • Automate complex reporting jobs.
  • Incorporate with other applications.

[]You can access the API reaction utilizing numerous different approaches, consisting of Java, PHP, and JavaScript, however this article, in particular, will focus on accessing and exporting data utilizing Python.

[]This short article will simply cover a few of the methods that can be utilized to access various subsets of data utilizing various metrics and measurements.

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

Establishing The API

Producing A Google Service Account

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

[]As soon as this has been developed, the next action is to select the + Create Service Account button.

Screenshot from Google Cloud, December 2022 You will then be promoted to add some details such as a name, ID, and description.< img src= "// www.w3.org/2000/svg%22%20viewBox=%220%200%201152%201124%22%3E%3C/svg%3E"alt="Service Account Particulars"width="1152"height=" 1124"data-src="https://cdn.searchenginejournal.com/wp-content/uploads/2022/12/screenshot-2022-12-12-at-20.20.21-639b81474320f-sej.png"/ > Screenshot from Google Cloud, December 2022 Once the service account has actually been created, browse to the KEYS section and add a new secret. Screenshot from Google Cloud, December 2022 [] This will trigger you to create and download a personal secret. In this circumstances, choose JSON, and then create and

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

Contribute To Google Analytics Account

[]You will also want to take a copy of the email that has been created for the service account– this can be discovered on the main account page.

Screenshot from Google Cloud, December 2022 The next action is to add that email []as a user in Google Analytics with Expert authorizations. Screenshot from Google Analytics, December 2022

Making it possible for The API The final and perhaps essential step is ensuring you have allowed access to the API. To do this, ensure you remain in the correct project and follow this link to enable gain access to.

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

Screenshot from Google Cloud, December 2022 This is needed in order to access the API. If you miss this action, you will be prompted to finish it when very first running the script. Accessing The Google Analytics API With Python Now everything is established in our service account, we can begin writing the []script to export the information. I chose Jupyter Notebooks to produce this, but you can also use other integrated developer

[]environments(IDEs)consisting of PyCharm or VSCode. Putting up Libraries The initial step is to set up the libraries that are required to run the rest of the code.

Some are unique to the analytics API, and others work for future sections of the code.! pip install– upgrade google-api-python-client! pip3 install– upgrade oauth2client from apiclient.discovery import construct from oauth2client.service _ account import ServiceAccountCredentials! pip set up link! pip set up functions import link Note: When utilizing pip in a Jupyter note pad, include the!– if running in the command line or another IDE, the! isn’t required. Producing A Service Build The next step is to set []up our scope, which is the read-only analytics API authentication link. This is followed by the client secrets JSON download that was produced when developing the private key. This

[]is used in a comparable way to an API key. To quickly access this file within your code, guarantee you

[]have actually saved the JSON file in the same folder as the code file. This can then quickly be called with the KEY_FILE_LOCATION function.

[]Lastly, include the view ID from the analytics account with which you would like 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 = [‘ https://www.googleapis.com/auth/analytics.readonly’] KEY_FILE_LOCATION=’client_secrets. json’ VIEW_ID=’XXXXX’ []Once we have included our personal essential file, we can add this to the credentials operate by calling the file and setting it up through the ServiceAccountCredentials action.

[]Then, established the build report, calling the analytics reporting API V4, and our currently defined qualifications from above.

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

Writing The Demand Body

[]As soon as we have whatever established and specified, the genuine enjoyable starts.

[]From the API service build, there is the ability to pick the aspects from the reaction that we want to access. This is called a ReportRequest things and requires the following as a minimum:

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

[]View ID

[]As pointed out, there are a couple of things that are needed throughout this construct phase, starting with our viewId. As we have actually already defined previously, we simply require to call that function name (VIEW_ID) rather than adding the entire view ID once again.

[]If you wished to collect data from a different analytics see in the future, you would simply need to change the ID in the preliminary code block instead of both.

[]Date Range

[]Then we can include the date range for the dates that we want to collect the data for. This consists of a start date and an end date.

[]There are a number of methods to write this within the construct request.

[]You can choose defined dates, for instance, in between 2 dates, by adding 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 last 1 month, you can set the start date as ’30daysAgo’ and completion date as ‘today.’

[]Metrics And Measurements

[]The final step of the basic response call is setting the metrics and dimensions. Metrics are the quantitative measurements from Google Analytics, such as session count, session duration, 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 short article, but they can all be discovered together with extra details and attributes here.

[]Anything you can access in Google Analytics you can access in the API. This includes objective conversions, begins and values, the browser device used to access the website, landing page, second-page course tracking, and internal search, website speed, and audience metrics.

[]Both the metrics and measurements are included a dictionary format, using key: worth pairs. For metrics, the secret will be ‘expression’ followed by the colon (:-RRB- and then the value of our metric, which will have a specific format.

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

[]With measurements, the secret will be ‘name’ followed by the colon once again and the value of the dimension. For instance, if we wished to draw out the various page paths, it would be ‘name’: ‘ga: pagePath’.

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

[]Integrating Dimensions And Metrics

[]The real worth is in integrating metrics and measurements to extract the crucial insights we are most thinking about.

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

action = service.reports(). batchGet( body= ‘reportRequests’: [‘viewId’: VIEW_ID, ‘dateRanges’: [‘startDate’: ’30daysAgo’, ‘endDate’: ‘today’], ‘metrics’: [‘expression’: ‘ga: sessions’], ‘dimensions’: [‘name’: ‘ga: medium’]] ). perform()

Developing A DataFrame

[]The response we get from the API remains in the type of a dictionary, with all of the information in key: value sets. To make the information much easier to view and analyze, we can turn it into a Pandas dataframe.

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

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

[]This will extract the information and include 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(‘data’, ). get(‘rows’, [] for row in rows: dimensions = row.get(‘dimensions’, [] dateRangeValues = row.get(‘metrics’, [] for header, measurement in zip(dimensionHeaders, measurements): dim.append(measurement) for i, worths in enumerate(dateRangeValues): for metricHeader, value in zip(metricHeaders, values.get(‘worths’)): metric.append(int(value)) []Adding The Response Data

[]When the information is in those lists, we can easily turn them into a dataframe by specifying the column names, in square brackets, and designating the list worths to each column.

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

< img src= "https://cdn.searchenginejournal.com/wp-content/uploads/2022/12/screenshot-2022-12-13-at-20.30.15-639b817e87a2c-sej.png" alt="DataFrame Example"/ > More Reaction Demand Examples Several Metrics There is likewise the capability to combine numerous metrics, with each pair added in curly brackets and separated by a comma. ‘metrics’: [, “expression”: “ga: sessions”] Filtering []You can also request the API reaction just returns metrics that return specific criteria by including metric filters. It utilizes the following format:

if metricName operator comparisonValue return the metric []For instance, if you only wished to draw out pageviews with more than 10 views.

action = service.reports(). batchGet( body= ‘reportRequests’: [] ). perform() []Filters likewise work for dimensions in a comparable way, however the filter expressions will be a little various due to the particular nature of measurements.

[]For instance, if you only wish to draw out pageviews from users who have checked out the website 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’: [], ‘metrics’: [], “dimensions”: [“name”: “ga: browser”], “dimensionFilterClauses”: [“filters”: [“dimensionName”: “ga: internet browser”, “operator”: “EXACT”, “expressions”: [” Chrome”]]]] ). perform()

Expressions

[]As metrics are quantitative procedures, there is also the ability to compose expressions, which work similarly to determined metrics.

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

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

response = service.reports(). batchGet( body= ‘reportRequests’: [‘viewId’: VIEW_ID, ‘dateRanges’: [], “metrics”: [ga: users”, “alias”: “conclusions per user”]] ). carry out()

Histograms

[]The API likewise lets you pail measurements with an integer (numerical) value into varieties utilizing histogram pails.

[]For example, bucketing the sessions count dimension into 4 pails of 1-9, 10-99, 100-199, and 200-399, you can use the HISTOGRAM_BUCKET order type and define the ranges in histogramBuckets.

action = service.reports(). batchGet( body= ). carry out() Screenshot from author, December 2022 In Conclusion I hope this has offered you with a basic guide to accessing the Google Analytics API, writing some various requests, and gathering some meaningful insights in an easy-to-view format. I have included the develop and request code, and the bits shared to this GitHub file. I will enjoy to hear if you try any of these and your plans for checking out []the information further. More resources: Featured Image: BestForBest/Best SMM Panel