Google Analytics

Google Analytics integration guide

Google Analytics is part of the Google Marketing Platform. You can start to track the performance of your websites for free. Get a complete overview of visitors to your websites, track goals, events, or segments on demographics or other information.

In this guide, we'll show you how to visualize your site's traffic data from Google Analytics in Clarify.

👍

Register and setup Google Analytics

If you are not already using Google Analytics, you can easily verify your site and start using Google Analytics for free here.
Visit Google Analytics

After you have set up your domain in Google Analytics you might need to wait a few days for it to collect traffic data (depending on your site's traffic volume and data delays).

Here is a brief overview of the traffic data we'll focus on in this guide to get you started:

Visitors
Users who have initiated at least one session during the date range.

Sessions
Total number of Sessions within the date range. A session is the period of time a user is actively engaged with your website, app, etc. All usage data (Screen Views, Events, Ecommerce, etc.) is associated with a session.

Bounce rate
The percentage of single-page sessions in which there was no interaction with the page.

Average session duration
The average length of a Session.

Google Analytics Dashboard exampleGoogle Analytics Dashboard example

Google Analytics Dashboard example

Getting started

1. Authentication

We use Google Analytics APIs to retrieve our data. Every request to the API needs to be authenticated so you need to use OAuth 2.0 to achieve this.

OAuth 2.0 scopes for Google Analytics
View your Google Analytics data: https://www.googleapis.com/auth/analytics.readonly

You can find more scopes available in the Google Analytics API scope list.

If you use Google Cloud Platform to manage the email accounts for your company you might need to setup new credentials, make sure that your user has access to the Google service and that the APIs is enabled.

2. Node-RED setup

If you are new to Node-RED or haven't checked out our Node-RED guides yet, we recommend that you check out Introduction to Node-RED before continuing.

Install google oauth2 node-red node

In this guide we'll use the node-red-contrib-google-oauth2 (unofficial) node to authenticate and communicate with Google.

In your node-red dashboard select Manage palette from the main top right hamburger button. View the Install tab and search and install the node as displayed in the screenshot below.

Installing OAuth2 node in Node-REDInstalling OAuth2 node in Node-RED

Installing OAuth2 node in Node-RED

If you have successfully installed the node you'll now have the node available in the left sidebar in Node-RED as in the screenshot below.

Node-RED Google nodeNode-RED Google node

Node-RED Google node

Configuring the Google node

Drag the Google node to your workflow, double-click, and select the edit button for "Connection".
Enter your Client ID and Client Secret which you obtained as part of the authentication step.
In this example, we use the scope for read-only access.
When you are ready you can press "Start Authentication" to authenticate with your Google account.

Google node credentials setupGoogle node credentials setup

Google node credentials setup

In this example we'll use the analytics:v3 API and the operation named data.ga.get.

3. Setup Query

In this step, we'll build the query which is used by the API to request the data we need. You can find more information in the Google API Reference.

Node-RED flow with Inject, Build Query & Google nodesNode-RED flow with Inject, Build Query & Google nodes

Node-RED flow with Inject, Build Query & Google nodes

Start by dragging an Inject node and "Function"-node from the left sidebar to your workflow and connect them.

📘

Tip

In Node-RED you can drag a "Debug"-node and connect it to nodes to display the output data from that node. In the screenshot above you could connect the "Debug"-node to the "Build Query"-node to preview the output before connecting it to the "google"-node.

Double-click on the Function node and rename it (E.g. to Build Query) and edit the following example Javascript to fit your use case. You need to at least edit YOURVIEWID with the VIEW ID in Google Analytics.

How to find your View ID

Select a view by clicking on the dropdown next to the top left Google Analytics logo.
When you are viewing the correct view go to Admin in the main navigation and click on View Settings and you'll find the View ID under Basic settings.

msg.metrics = ["ga:users","ga:sessions","ga:bounceRate","ga:avgSessionDuration","ga:pageviews"];

msg.payload = {
    "ids": "ga:YOURVIEWID",
    "start-date": "7daysAgo",
    "end-date": "today",
    "metrics": msg.metrics.join(),
    "dimensions": "ga:date"
};

return msg;

In this example, we'll set msg.metrics to be an array of the metrics we want to receive. We use start-date and end-date to specify the time range we want to receive data from. We also set dimensions to ga:date to receive rows of dates with data.

The query is applied to the msg.payload object because this object is used by the Google node in the next step to perform the API call.

When you have tested that the query looks Ok, connect the Build Query node to the Google node.

4. Prepare Data

In this step, we'll verify the data response from the Google Analytics API and prepare the data for Clarify.
Connect a Debug node to the Google node (make sure the node is configured with "Output complete msg object").
After you have connected the Debug node you can press the trigger button in front of the Inject node to trigger the flow.

Node-RED debug node trigger buttonNode-RED debug node trigger button

Node-RED debug node trigger button

A short summary of our current progress before we look into the output from Google. We now have:

  1. The Inject node which triggers the flow
  2. Build Query node which creates the query parameters we send to the Google Analytics API.
  3. Google node to authenticate and perform Google Analytics API requests.
  4. Debug node to display the result from the Google Analytics API.

Below you'll find a sample response from Google which we will re-format and send to Clarify.
(We have removed some information from the response to make it easier to read)

{
   "_msgid":"cdd76d29.cdafb",
   "payload":{
      "rows":[
         [
            "20210422",
            "18",
            "23",
            "47.82608695652174",
            "223.56521739130434",
            "65"
         ],
         [
            "20210423",
            "10",
            "15",
            "73.33333333333333",
            "41.666666666666664",
            "18"
         ],
      ]
   },
   "topic":"",
   "metrics":[
      "ga:users",
      "ga:sessions",
      "ga:bounceRate",
      "ga:avgSessionDuration",
      "ga:pageviews"
   ]
}

To continue, drag a Function node to your workflow, double-click the node, and name it "Prepare Data". Again, you can drag a Debug node and connect it to your newly created node to verify that the data is formatted correctly before being sent to Clarify.

Node-RED with newly added Prepare Data and Debug nodesNode-RED with newly added Prepare Data and Debug nodes

Node-RED with newly added Prepare Data and Debug nodes

Example Javascript which can be used in the Prepare Data node to format data for Clarify.

const DateTime = global.get('luxon').DateTime;
let records = msg.payload.rows;

msg.metrics.forEach((metric, index)=> {
    let dates = [];
    let values = [];
    
    if (records.length > 0) {
        records.forEach((item)=> {
            if (item[0] && item[1]) {
                let date = DateTime.fromISO(item[0]);
                dates.push(date)
                values.push(parseFloat(item[index+1]));
            }
        });
    }
    msg.id = "google-analytics-" + metric.toLowerCase().replace(":","-"); 
    msg.signal = {
        name: "Google Analytics " + metric,
      labels: {
            "data-source": ["Google Analytics"],
        }
    }
    msg.payload = {
            "times": dates,
            "values": values,
    }
    
    node.send(msg);
});

return null;

The code above will send a message and create a signal in Clarify for each metric we have specified.

We use the NPM package Luxon in the code above to work with date formats. You'll need to install this package or use a different package to handle dates. You can also check out the Node-RED guide.

5. Send data to Clarify

In this step, we'll send the actual data to Clarify. As a preparation, you need to follow the guides Installing the Clarify nodes and Using the Clarify Insert node to make sure your workflow is able to correctly send data to Clarify.

When you have installed the Clarify nodes and setup credentials you can connect the Prepare Data node to the Clarify node. We also recommend that you connect two Debug nodes to the Clarify node. One for Errors and the other for other debug messages.

Final flow in Node-REDFinal flow in Node-RED

Final flow in Node-RED

📘

Automatically trigger flow

The Inject node supports automatic triggering at certain intervalls. You can achieve this by double-clicking on the Inject node and configuring the Repeat setting. For example by setting "repeat interval" with "1 hour" the workflow above will trigger and update data every hour.

6. Visualize data in Clarify

When our workflow has run we now have 5 new signals in Clarify which contain data for Sessions, Users, Bounce Rate, Pageviews & Average session duration.
To visualize the data we need to publish the signals as Items and create a new timeline.

If you follow the guide, you can now create a new timeline with our fresh data. It might look similar to the screenshot below.

Drag the Google Analytics Items to the timeline and personalize the timeline with labels and colors that suit you and your company.

Congratulations

You have now made your Google Analytics data more available and easier to explore. The next step in Clarify would be to invite your teammates to the timeline so that you can collaborate around this data.


Did this page help you?