We are well aware of a fact that Google Analytics 360 has real-time data available. But not everyone is aware that if you want to export this data, Google Analytics 360 does not allow its users to export all rows. The maximum number of rows that you can export is limited to only the top 10 values of the dimension that you’re looking at.
Google Analytics has also provided Real Time Reporting API which you can use to pull all the data, and if you are a developer, you can do so with any scripting language and create a dashboard. But, in this blog, we are going to see how you can pull all your data into a single Google Sheet with Google Apps Script with minimal effort. One of the biggest advantages of working with Apps Script is that you are already in a Google Authenticated environment. So, you need not worry about the authentication handshake since all the users will be authenticated by Google itself. This means you can access Real Time data with just a few lines of code. There are only a couple of configurations that you first need to achieve and that’s it, you are done.
Setting up you project.
- Create a new Google Spreadsheet.
- Click on Tools and from the drop down menu choose Script Editor.
- The above operation will open a Script Editor in new tab.
4. Our task is to fetch the Google Analytics real-time data for which we have to enable Analytics API.
5. To enable Analytics API, click on Resources -> Advanced Google Services.
6. You will be prompted for New Project Name. Give a desired name and then pop up for Advanced Google Services will be listed.
7. Choose Google Analytics API and enable it.
8. You need to enable this API also in your Google Developers Console.
9. Click on Google API Console.
10. Click on ENABLE APIS AND SERVICES.
11. Search for Analytics API and enable it.
12. Here we shall pull dimensions Source & Medium, and Active user as Metric. Go back to your Script Editor and use the below snippet of code with a couple of changes as listed below:
a. Change your Google Analytics View ID
b. Change your Google Spreadsheet ID
13. Now, click on Run to run the script check in your Spreadsheet your data would be populated.
Below are reference documents which will help you to pull different dimensions:
Benefits of using Apps Script for fetching Google Analytics Real Time Data
- No need to write code for user authentication as the same will be handled by Google.
- You can start taking decisions in the meantime you have access to real time data.
- Utilize this as a data source in Data Studio to build more enhanced dashboards.
- Automate script execution by setting a simple time based trigger.
- Set up custom mail notification with App Script to get notified if something is found wrong with the existing data.
Note: Do check the quota limits of Real Time API and set trigger to refresh data accordingly.
We are now familiar with using Real Time API with App Script to fetch real time data from Google Analytics. But this is not just an end yet. You can also build more enhanced dashboards or even a product with this script. You can run API query in different ways to fetch data in sorting or in a particular range order just the way you wish to explore.