.NET Core & the Google Analytics Real Time Data API

.NET Core & the Google Analytics Real Time Data API

At Wehkamp, we use Google Analytics extensively to measure how many users are active. It is hard to correlate active users with your other statistics that "live" in Prometheus. In this blog I'll show how you can use the Google Real Time API to produce statistics for active users, page views and screen views.

A big shout-out to our Technical Web Analyst Byrge Leeuwangh for the collaboration on the setup.

1. Google setup

First, we need to set up a new project in Google Cloud Console and add a service account to it. Next we'll add the account to our Google Analytics view.

Google Cloud Console project

To access the Google Analytics API, we need to create a Google Cloud Console project first. Make sure you have a dev account with Google Cloud Console. The following steps will create a new project:

Add a service account

Now, let's create a service account that will be used in your application. A service account is a non-personal account that is attached to your project. You create one like this:

Add service account to Google Analytics

So, we've created a service account and downloaded the keys and copied the email address. Now, we need to link the service account to the Google Analytics view:

2. Notes on the Google Analytics API

We are interested in recording the following metrics:

Did you know you can try this out online? The docs have a nice 'Try this API' feature. Check the Real Time Data: get API docs.

Quotas

Beware of the Google Analytics API quotas:

What are the maximum quotas for Real Time Reporting API requests? 50,000 requests per project per day and 10 queries per second (QPS) per IP address. Google Analytics Core Reporting API FAQs

Google Analytics Core Reporting API FAQs

In our case, we are doing 4 calls to 2 views. So if we do one run of calls every 15 seconds, we do:

Consider creating multiple projects (per environment), so you don't hit your account limits (as we did). If you ever hit the quota; it will reset at midnight Pacific Time (PT).

3. .NET Core Implementation

Let's keep it simple and build a .NET Console App. Our implementation is based on this NuGet package from Google:

dotnet add package Google.Apis.Analytics.v3 --version 1.51.0.1679

Replace the 98765432 number of the viewId variable with your own number. Copy the downloaded service account JSON file to the same directory as your application, rename it to credentials.json and make sure it is copied to the output directory.

using Google.Apis.Analytics.v3;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using System;
using System.Threading.Tasks;

class Program
{
    async static Task Main(string[] args)
    {
        var credentialFilePath = "credentials.json";
        var credential = GoogleCredential.FromFile(credentialFilePath);
        var service = new AnalyticsService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential.CreateScoped(
                AnalyticsService.Scope.AnalyticsReadonly
            ),
            ApplicationName = "google-analytics-real-time-api-in-net-core",
        });


        var viewId = "ga:98765432";
        var metric = "rt:activeUsers";
        var request = service.Data.Realtime.Get(viewId, metric);
        var response = await request.ExecuteAsync();
        var activeUsers = 0;

        if (response.Rows != null)
        {
            activeUsers = Convert.ToInt32(response.Rows[0][0]);
        }

        Console.WriteLine($"{activeUsers} active users.");
    }
}

Final thoughts

We've built a .NET Core API with a background service that polls the Google Analytics Real Time Data API. We're using Scrutor and decoration to convert the results to Prometheus metrics. We're plotting the metrics with Grafana. This will give us better insights during SRE.

And... we've added the Grafana dashboard to our DevOps Slack bot:

We're using our DevOps Slack bot to query Grafana views with the results of the Google Analytics Real Time Data API.