Thursday, May 9, 2024
HomeWeb AnalysisHow to set client id for custom dimension in AMP Analytics?

How to set client id for custom dimension in AMP Analytics?

The method of setting client IDs and timestamps in custom dimensions in regular Google Analytics and analyzing time-series access data for each user has gradually become common.

Then, is it possible to set the client ID and timestamp as custom dimensions in the same way in Google Analytics under the AMP environment where JavaScript cannot be executed? The answer is yes. You can also check that the client ID and timestamp are set as custom dimensions on the AMP page of individual articles on this blog (change the end of the URL to .amp to open the AMP page).

This article explains how to set the Client ID and Timestamp for custom dimensions in the AMP version of Google Analytics.


What is a Client ID

A client ID is an ID used to uniquely identify a user (strictly speaking, a browser) on the Google Analytics platform, and is stored in a browser cookie. Sent together with Google Analytics tracking data. However, in Google Analytics without any customization, this client ID can only be seen in the “User Explorer” report, and it is a difficult-to-use item that cannot be output as CSV (in order to show it to the user in the first place It can not be helped because it is not an item used for ).

With such a client ID, it is possible to improve usability by customizing it yourself using a function called custom dimension. By setting the client ID and timestamp in the custom dimension, you will be able to retrieve raw data that has not been aggregated (strictly speaking, each index is 1 as a result of aggregation). We are starting to see cases where this raw data is analyzed using BI tools such as Tableau and QlikView.

In addition, the method of setting the client ID as a custom dimension in normal Google Analytics was written in the previous article ” How to obtain the Google Analytics ClientID using GTM ” as a Google Tag Manager version , so refer to that. please.

Setting method in AMP Analytics

The AMP version of Analytics has the major limitation of not being able to use JavaScript, so the method of setting the client ID in a custom dimension in the regular version of Google Analytics cannot be used as is. You need to customize it for the AMP version of Google Analytics.

There are two ways to use Google Analytics in an AMP environment: writing tracking code using JSON code and using Google Tag Manager. In both of these, it is possible to set Client ID and Timestamp as custom dimensions. However, please note that it is currently difficult to set the timestamp in a format that formats the year/month/day/hour/minute/second in an easy-to-understand format.

Also, no matter which method you use, you need to create “Client ID” and “Timestamp” in custom dimensions, so please create them in advance.

When using JSON code

To customize tracking code using JSON code, you need to understand how this JSON code works. To understand how it works, I recommend reading the official help for developers – Adding analytics to AMP pages – Extending googleanalytics .

Also, the client ID and timestamp are pre-defined as AMP variables in the <amp-analytics> tag, so no preparation is required and you can use them as they are.

Now, without going into detail, here is the JSON code for tracking with the client ID and timestamp. In the example below, the client ID is set to custom dimension 1 and the timestamp is set to custom dimension 2. I think that the index number will change depending on each property, so please change it as appropriate.

<amp-analytics type="googleanalytics" id="analytics1">
<script type="application/json">
{
  "requests": {
    "pageviewWithClientID": "${pageview}&cd1=${clientId(AMP_ECID_GOOGLE)}&cd2=${timestamp}"
  },
  "vars": {
    "account": "UA-XXXXX-Y"
  },
  "triggers": {
    "trackPageviewWithClientID" : {
      "on": "visible",
      "request": "pageviewWithClientID"
    }
  }
}
</script>
</amp-analytics>

A quick explanation of the JSON code is to prepare a request URL template that uses custom dimensions in the requests object. This template basically uses the request URL of the page view as it is, but only information about custom dimensions is added because it does not exist by default. At this time, the defined variable can be used as it is for the value. A list of predefined variables is compiled at Github ampproject/amphtml – AMP HTML URL Variable Substitutions . According to this, to get the client id,

${clientId(COOKIE_NAME)}

, so check the name of the cookie being used. Looking at the cookie value in Chrome’s developer tools, it seems that the client ID is stored in a cookie named “AMP ECID GOOGLE”, so the client ID is

${clientId(AMP_ECID_GOOGLE)}

can be obtained at

If you are also doing event tracking, you need to generate a request URL for event tracking as well.

Once the implementation is complete, we will test the operation. A quick test is to use a developer tool such as Chrome to directly see the HTTP requests you’re sending.

When using Google Tag Manager

In October 2016, Google Tag Manager became available for AMP sites as well. There are many things you can’t do compared to the regular web version of Google Tag Manager, but you can use custom dimensions, and it also supports client IDs and timestamps.

create a variable

First, create two variables, a client ID and a timestamp. In order to get the client ID with the web version of Google Tag Manager, it was necessary to initialize the tracker using a custom HTML tag and use the dataLayer variable, but with the AMP version of Google Tag Manager, You can use it only by using the variable type “AMP variable”.

Variable name Variable type AMP variable name
Client ID AMP variables clientId(AMP ECID GOOGLE)
Timestamp AMP variables timestamp

Please pay attention to the difference between uppercase and lowercase letters. The capture is below.

Variable type selection screen. Here, select “AMP Variables”
In the variable “Client ID”, enter “clientId (AMP ECID GOOGLE)” in the AMP variable name
For the variable “Timestamp” put “timestamp” in the AMP variable name

Set to Google Analytics tag

The tag setting method is the same as the regular web version of Google Tag Manager. Select “Google Analytics” from the tag template and set “Index number” and “Value” in the “Custom dimension” item of the template. In the case of the AMP version of Google Tag Manager, the items that can be set in the template are greatly reduced, but it seems that custom dimensions and custom indicators are somehow supported.

The capture of the set state is as follows.Capture 004

summary

In this article, I explained how to set the client ID as a custom dimension as an example of advanced customization that can be done even when using the AMP version of Google Analytics, which has large implementation restrictions. There are many other customizations that can be realized with AMP analytics, so we will continue to conduct research and experiments and explain them here.

RELATED ARTICLES

Leave a reply

Please enter your comment!
Please enter your name here

Recent Posts

Most Popular

Recent Comments