#GTMTips: Write the client ID and other GTAG fields in the data layer – News Couple

#GTMTips: Write the client ID and other GTAG fields in the data layer

One of the most significant omissions, at least for now, in Google Analytics 4 is customTask. It’s unfortunate, but there is no such mechanism in the client-side SDK.

This means that you will not be able to do all magical things customTask Enabled in Universal Analytics. One of the biggest problems is how to collect very useful fields like CustomerID, as such Not Available by default in the Google Analytics 4 reporting interface.

Well, with a new custom tag template, you can now fetch these values ​​from GTAG and write them in it dataLayer, and use them in Google Analytics 4 as custom parameters, eg.

It is not as easy to withdraw as it used to be customTask, but it gives you an added bonus: it’s where the data is written dataLayer, it can be used with other tags as well and not just with Google Scripts.


Simmer . Newsletter

Subscribe to the Simmer newsletter to get the latest news and content from Simo Ahava right in your inbox!

Tip 121: Type the CustomerID and other GTAG fields into the dataLayer

For any of this to work, you Should Implement a Google Analytics 4 batch (or Ads/Floodlight) on the page, preferably via Google Tag Manager using default forms.

Bring the template and create a tag

First, go to templates In the Google Tag Manager UI, search for GTAG GET API in the template gallery. Once you find the form, click Add to workspace to install the form.

After that, go to Tags And create a new tag. use the GTAG GET API Model as tag type. This is what you should see on the screen now:

GTAG GET API Default Settings

tag composition

Designation GA4 Measurement ID in the appropriate field. If you wish to collect GCLIDs for ads or Floodlight tags, use the relevant measurement ID instead.

Next, select the fields whose values ​​will be fetched. Customer ID (The unique identifier of the browser used by GA4) is a no-brainer, and although the session identifier is automatically collected to GA4, you may still want to send it to other vendors.

If you set custom Fields Using the Fields to set option in your config tag, you can also add these fields to a file Custom fields to get table.

This is what the component tag might look like:

GTAG Templates API

Create the tag sequence

the following, And that’s important, you need to add this tag in the tag sequence, so that it is activated after, after is yours GA4 initialization tag (or the event tag, if you’re not using the config tag).

It is very important that you do it this way. It’s the only way to guarantee that the API is ready to accept requests.

To add the tag in the sequence, open a file Google Analytics 4: Configuration tick , then scroll to Advanced Settings -> tag sequence.

Check the box next to Launch tag after… fires, and select the tag you created from the custom template in the dropdown menu. This is what it would look like:

gtag tag sequence

By using tag sequences, you ensure that the API is not called until afterwards gtag() It is loaded and configured on the page.


If you now go into preview mode and test your setting, you’ll see a new data layer event named gtagApiGet in the event list.

GTAG API Preview

If you don’t see it, you most likely don’t have a GA4 config on the page, or because the API tag has already been activated gtag() He had time to download.

Create the trigger and variables

To make use of this information, you need to create a custom event trigger for gtagApiGet event plus data layer variables for each field whose value you want to take advantage of.

Custom event trigger for gtagApiGet

Custom event trigger for gtagApiGet

Data layer variable for gtagApiResult.client_id

Data layer variable for gtagApiResult.client_id

Create a new Google Analytics 4 event tag

Finally, if you want to send these values ​​to Google Analytics 4, you can. You need to choose whether you want to use a user property or custom event dimensions. For user domain-specific fields like client ID, the former makes more sense, and for event-specific fields, the latter may be more useful.

Unfortunately, GA4 in all its wisdom forces any numeric fields to be converted to number types, so the CID is rewritten as Double. This means that it becomes illegible because there are too many decimals and the result is broken.

I have now solved this by adding a simple . At the end of the client id in the event tag itself (see below). This is easy enough to overlook in reports or clean up in BigQuery Export. If you have another solution for this, please add it to the comments of this article!

This is what the final event tag might look like:

GA4 event tag

You can always use DebugView to validate the setting.


You see, little one . At the end of the client id is there to make sure that GA4 doesn’t convert this string to a number.


I miss customTask. much. I really hope Google offers something similar to gtag()But I am not optimistic. this dimension many One of the things we did previously in the client was passed over to the Google Analytics 4 admin (cross-domain tracking, for example).

I feel like Google wants to simplify the implementation part as much as possible, which makes a lot of sense. But she won’t do that Harm For customization options for power users.

In this article I showed you how to fetch the fields for GA4 using an API for gtag(). The API itself is a bit tricky to use because it is asynchronous. This means that it will not work in a variable as the variables depend on synchronous execution.

The silver lining is that the data is now available in a . format dataLayer For all your tags, not just the ones that run on the Google stack.

Hope you found this helpful – please let me know in the comments if something is unclear about how to use the form or .get() API works!

Source link

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button