How to use Google Analytics API without any coding

In this article I will show you how to use Google Analytics API without doing any coding.

Introduction to APIs

Application programming interface (or API) is the interface between two program modules. This interface is made up of set of routines, protocols and tools. Through API you can:

  1. Create new software applications.
  2. Integrate your application with a third party application.
  3. Enhance the functionality of an existing application.

Every program has to depend upon another program in order to carry out some of its functions.

For example your web browser has to depend upon an operating system in order to work. Your operating system has to depend upon system applications in order to work.

In fact API itself has to depend upon ABI (Application binary interface) in order to work. Without APIs, you need to create all of the functionality from scratch and that too at the binary level (01010101).

There are many different types of APIs. There are APIs used by operating systems, APIs used by non-system applications, APIs used by websites (Web APIs) etc.

A Web API is an API which defines the interface between a web browser and web server.

If you want to use the functionality provided by Facebook in your program/application, then you need to write a program that can interact with Facebook API by making a request called API call.

You need to go through the developers documentation provided by Facebook in order to understand how you can make your program interact with Facebook API.

Similarly, if you want to use the functionality provided by Google Analytics, then you may need to write a program that can interact with Google Analytics API. 

 

Introduction to Google Analytics APIs

Google Analytics provides two types of APIs:

#1 Management API

#2 Core Reporting API

Through management API you can access information related to your Google Analytics accounts (like account ID), web properties and views. But you can’t access actual report data through Management API.

To access the actual report data (like sessions, pageviews, bounce rate etc) you need to use the Core Reporting API.

Following are some of the advantages of using Google Analytics APIs:

#1 Query and manage dozens of analytics properties/websites at once.

#2 Merge Google Analytics data with other data sources.

#3 Retrieve information in almost any way you want.

#4 Schedule Google Analytics exports to Excel, Big Query etc.

#5 Automate dashboards and reports creation.

#6 Reduce/eliminate data sampling through query partitioning. 

#7 Automate the import of cost data into Google Analytics

#8 Simplify data extraction, integration and visualization.

#9 and the list goes on …….

You indirectly access both management API and core reporting API without writing any code through the online software provided by Google Analytics. Every time you access one of the report of Google Analytics, you are indirectly using the Google Analytics APIs.

However if you want to access Google Analytics APIs directly, for example access Google Analytics data directly into your spreadsheet, you need to write a code which can make a request to Google Analytics API.

This is where many people struggle.

Writing a code which can make API calls is not easy. So in order to facilitate this process there are number of third party tools out there (like Analytics Canvas) which make it possible to directly interact with Google Analytics APIs without writing any program.

In this article I will show you how to access Google Analytics APIs without doing any coding via the Google Analytics Spreadsheet add-on.

In order to use this add on you need to first get familiar with:

#1 Google Analytics Account Explorer

#2 Google Analytics Dimensions and Metrics Explorer

#3 Core Reporting API Dimensions and Metrics

#4 Core Reporting API Filters

#5 Google Analytics Query Explorer

 

Google Analytics Account Explorer

It is a tool through which you can identify the Google Analytics accounts, web properties and views you have got access to and most importantly you can identify IDs (account IDs, web property IDs, view IDs and table IDs) which are required in order to access GA via APIs.

In order to use this tool, follow the steps below:

Step-1: Go to Google Analytics account explorer: https://ga-dev-tools.appspot.com/account-explorer/

ga account explorer

Step-2: Add this tool to your Google Analytics Account by clicking on the ‘Click here to authorize’ link:

click here to authorize

request for permission

Note: You need this authorization in order to use Account and Query explorer tools.

Step-3: Select your Google Analytics account, web property and view from the drop down menus:

search account information

You can now see the various IDs (account IDs, web property IDs, view IDs and table IDs) related to your GA account.

 

Google Analytics Dimensions and Metrics Explorer

It is a tool which list and explain all of the dimensions and metrics available via Core reporting API. It also tells you which dimensions and metrics can be queried together.

In order to use this tool, follow the steps below:

Step-1: Go to Google Analytics Dimensions and Metrics explorer: https://ga-dev-tools.appspot.com/dimensions-metrics-explorer/

You can also access this tool by clicking on the ‘Dimensions and Metrics’ explorer link from the left hand navigation menu while using the Google Analytics account explorer tool:

dimensions and metrics explorer

Note: ‘Account explorer’, ‘Query Explorer’ and ‘Google Analytics spreadsheet Add-on’ tools are also accessible via this navigation menu.

 

Step-2: Click on the ‘Launch Tool’ button as shown below:

dimensions and metrics explorer2

You will then see this page: https://developers.google.com/analytics/devguides/reporting/core/dimsmets which list and explain all the dimensions and metrics available via Core Reporting API:

dimensions and metrics explorer

Visit this page, take a look around and get familiar with various Core Reporting API dimensions and metrics.

 

Core Reporting API Dimensions and Metrics

When we access Google Analytics directly via API, we use the API name of a dimension/metric/segment instead of the web view name (also known as UI name):

web view name

Web view names (or User Interface names) are the names of dimensions, metrics and advanced segments you see in Google Analytics reports:

web view names2

 

Following are the corresponding API names:

api names google analytics

It is important that you understand the difference between web view name and API name. Otherwise you can’t access GA via API through Google Analytics Spreadsheet Add-on.

 

Core Reporting API Filters

In order to get meaningful data via Core reporting API, you must know, how to use and apply API filters. Following is the syntax of the core reporting API filter:

name operator expression

Here,

name – it is the API name of a Google Analytics dimension or metric to which you want to apply a filter. For example: ga:region, ga:country, ga:sessions etc.

operator –  it can be any dimension or metric filter operator listed below:

api filters

Note: These operators must be URL encoded before they can be included in a query string. For example the URL encoded form of == operator would be %3D%3D.

However if you are using ‘Query explorer’ or ‘GA spreadsheet add-on’ that you don’t need to URL encode the operators.

expression – it is a regular expression

For example:

ga:country==United States

This filter instructs the API to report only that hit data (pageview, screenviews, events, items etc) which is from US

Another example:

ga:region=~^c

This filter instructs the API to report the hit data of only those regions whose name start with the letter ‘C’.

Here =~ is one of the dimension filter operator and ^ is the caret sign which has got special meaning in regex.

 

API filters become most useful when they are combined with other filters. You can use AND and OR operators to combine two or more filters.

The AND operator is denoted by a semicolon ; whereas OR operator is denoted by comma ,

For example:

ga:country==United States;ga:region=~^c

This filter instructs the API to report only that hit data which is from US regions and whose names start with the letter ‘C’.

Another example,

ga:country==United States,ga:country==United Kingdom

This filter instructs the API to report only that hit data which is either from US or from UK.

 

Google Analytics Query Explorer

In order to access GA data via core reporting API, you need to build and execute queries. Through Google Analytics query explorer you can do that.

Just to show you how this tool can be used, I will reproduce the data from the following GA report via API:

organic traffic report

There are few things which you need to note about this report:

#1 This report list only the regions in US.

#2 This report list only those regions whose name start with the letter ‘C’. This was achieved by using an advanced filter on the reporting interface.

#3 A default advanced segment called ‘organic search’ is applied to this report.

#4 This report includes one dimension called ‘Region’ and 4 metrics: New users, % New Sessions, Sessions and Goal 4 value.

#5 This report sort the ‘New Users’ metric in descending order.

 

Follow the steps below to use the Query explorer tool:

Step-1: Go to Google Analytics Query explorer: https://ga-dev-tools.appspot.com/query-explorer/

Step-2: Select the Google Analytics account, property and view whose data you want to access via the API from the drop down menus:

query explorer

Step-3: Select ‘Start date’ and ‘end date’. This is the date range for which you want to see the data:

set the date range

set the date range2

Step-4: Select the metrics you want to include in your report from the drop down menu:

select metrics

select metrics2

Here I have selected the 4 metrics whose API names are: ga:newUsers, ga:percentNewSessions, ga:sessions and ga:goal4value in order to reproduce the data from the GA report above.

 

Step-5: Select your dimension (in this case ga:region) from the drop down menu:

select dimension

Step-6: Select the sorting order and the sorting direction from the drop down menu:

select sort

I selected new users descending (-ga:newUsers) because the GA report above, sort the ‘New Users’ metric in descending order.

Step-7: Enter the filter expression in the ‘filters’ box:

api filters2

I entered the following filter expression: ga:country==United States;ga:region=~^c because the GA report  (whose data i want to reproduce) list only those regions in US whose name start with the letter ‘C’.

Step-8: Select the ‘organic traffic’ segment from the drop down menu:

organic traffic segment

Step-9: Set the sampling level (to adjust data sampling) to DEFAULT, FASTER or HIGHER_PRECISION:

sampling level

I set the sampling level to DEFAULT by leaving the ‘sampling level’ parameter field blank.

 

Step-10: Set the ‘start index’ parameter. This parameter is used to specify the row number i.e. the row from where you want to start retrieving the GA data.

For example, if you have got say 1000 rows in your report and you want to retrieve data from the 900th row then set the ‘start index’ parameter to 900.

The default value of this parameter is 1 which can also be specified by leaving the ‘start index’ parameter field empty:

start index

Since I want to retrieve results from the beginning, I left the ‘start index’ parameter field empty.

 

Step-11: Set the ‘max-results’ parameter. This parameter denotes the maximum number of rows you want to include in your report. Since I don’t want to include more than 10 rows in my report, I set the max-results parameter to 10:

max-results
Step-12: Click on the ‘Run Query’ button. You will then see a report from the query explorer tool like the one below:

query explorer report

Note the data from this report matches with the Google Analytics report:

organic traffic report

 

Google Analytics Spreadsheet Add-on

Once you understand how to use the query explorer, you are in a good position to use the GA spreadsheet add-on. Through this add-on you can access GA data directly via core reporting API into a Google Spreadsheet.

Just to show you how this tool can be used, I will reproduce the data from the following GA report via the add-on:

organic traffic report

Follow the steps below to use this tool:

Step-1: Install Google Analytics Spread sheet add-on from here: https://chrome.google.com/webstore/detail/google-analytics/fefimfimnhjjkomigakinmjileehfopp

 

Step-2: Open a new Google Spreadsheet and then go to Add-ons menu > Google Analytics > ‘Create New Report’:

create new report

Step-3: Configure the new report like the one below:

configure the report

I have selected the metrics and dimension according to the GA report I want to reproduce.

Step-4: Click on the ‘create report’ button. You will then a see a report like the one below:

configure the report2

If you look closely at this report, you can see that following fields are empty:

  1. Start date
  2. End date
  3. Sort
  4. Filters
  5. Segment
  6. Max Results

You can get all of this data from ‘query explorer’. Once you have supplied all the required data, your Google spreadsheet may look like the one below:

configure the report3

Note: Here I deleted the ‘Last N Days’ field and changed the metric ga:goalXXValue to ga:goal4Value.

 

Step-5: Go to Add-ons menu > Google Analytics > ‘Run Report’:

run report

If there is no error in one of your query parameters, you will get a ‘completed successfully’ report status:

report status

If there is an error in one of your query parameters, you will get a ‘report failed’ report status:

report status failed

Note: You are less likely to get errors if you build your queries through query explorer first before you start using GA Spreadsheet add on.

 

Step-6: Click on the ‘ok’ button of the ‘Report Status’ dialog box. If there is no error in your query parameters then you will see a new tab added to your Google spreadshet. This tab contains your new report:

new tab

final report

Note the data from this report matches with the Google Analytics report:

organic traffic report

 

 

Announcement about my new books

Maths and Stats for Web Analytics and Conversion Optimization
This expert guide will teach you how to leverage the knowledge of maths and statistics in order to accurately interpret data and take actions, which can quickly improve the bottom-line of your online business.

Master the Essentials of Email Marketing Analytics
This book focuses solely on the ‘analytics’ that power your email marketing optimization program and will help you dramatically reduce your cost per acquisition and increase marketing ROI by tracking the performance of the various KPIs and metrics used for email marketing.

Attribution Modelling in Google Analytics and Beyond
Attribution modelling is the process of determining the most effective marketing channels for investment. This book has been written to help you implement attribution modelling. It will teach you how to leverage the knowledge of attribution modelling in order to allocate marketing budget and understand buying behaviour.

Himanshu Sharma

Certified web analyst and founder of OptimizeSmart.com

My name is Himanshu Sharma and I help businesses find and fix their Google Analytics and conversion issues.
If you have any questions or comments please contact me.

  • Over eleven years' experience in SEO, PPC and web analytics
  • Certified web analyst (master level) from MarketMotive.com
  • Google Analytics certified
  • Google AdWords certified
  • Nominated for Digital Analytics Association Award for Excellence
  • Bachelors degree in Internet Science
  • Founder of OptimizeSmart.com and EventEducation.com

I am also the author of three books: