About the gtm.uniqueEventId dataLayer variable

Something you'll probably never need, but here's how gtm.uniqueEventId works.

Justus

Justus

owntag Founder

published July 2, 2021

The gtm.uniqueEventId attribute is automatically added to of every dataLayer event by Google Tag Manager. It’s used for internal purposes of GTM and you can probably ignore it.
But here’s how it works:

If you push an event like this

window.dataLayer = window.dataLayer || []
window.dataLayer.push({
	event: "myExampleEvent"
})

this is what would end up in dataLayer:

{
	"event": "myExampleEvent",
	"gtm.uniqueEventId": 34
}

The 34 here is just an example. Even on your site, the IDs will vary depending on which and how many dataLayer events you have.

How gtm.uniqueEventId is calculated

The id is increased with a) every dataLayer event b) every time a variable is resolved

If you had an empty container without any variables and simply pushed events without doing anything with them, the value of gtm.uniqueEventId would increase sequentially from 1 to 2, 3, 4, 5 and so on.

You can demonstrate how the ID increased with every variable resolution by creating a tag that references multiple variables (or the same variable multiple times).
A custom HTML tag like this, for example:

The historychange event in GTM Preview Mode

If you execute the script with the All Pages trigger on the gtm.js event, the gtm.uniqueEventId of the subsequent gtm.dom event will have increased by 11:

The historychange event in GTM Preview Mode

An increase by 1 because of the gtm.dom event itself and another 10 for the 10 variable resolutions of {{Page Hostname}}.

You could also reverse the example. Here the GTM variable is resolved only once and its value then stored in a JavaScript variable:

The historychange event in GTM Preview Mode

Therefore, gtm.uniqueEventId is only increased by 2:

The historychange event in GTM Preview Mode

Uniqueness

Event though it has “unique” in its name, its obviously only unique on a per-pageload basis and there is no also no fixed connection between the type of event and its ID.
You should probably ignore it and look somewhere else if you are looking for a truly unique identifier.

If you are looking to generate an Event ID for the Facebook Conversion API, head over to Simo Ahava’s article on the matter where he explains how to use an ID generator.

Become a Server Side Tagging Pro with owntag

Take control of your digital data collection with Server Side Tagging and Server Side GTM – easily hosted with owntag.

App screenshot