Skip to main content

Data Layer

Using a data layer

As defined by Google:

A data layer is a JavaScript object that is used to pass information from your website to your Tag Manager container. You can then use that information to populate variables and activate triggers in your tag configurations.

The Google developer guide is an excellent starting point for understanding how to use a data layer and well worth reading.

Setting up the data layer on your page

Defining a data layer:

<head>
<script>
window.dataLayer = [{
ga4MeasurementId: 'G-XXXXXXXX'
}];
</script>
</head>

The above snippet will define a data layer. This approach is fine if you can be absolutely certain that nothing else will define a data layer before it. If a data layer was defined earlier, then this snippet would overwrite it.

A safer approach would be to use the following:

<head>
<script>
window.dataLayer = window.dataLayer || [];
window.dataLayer.push({
ga4MeasurementId: 'G-XXXXXXXX'
});
</script>
</head>

This ensures that if a data layer was previously defined, then we are adding to it rather than overwriting it.

Each of the variables declared within the data layer object will only persist as long as the visitor remains on the current page. Data layer variables that are relevant across pages must therefore be declared in the data layer on each page of your website. While you don't need to put the same set of variables in the data layer on every page, you should respect the naming convention.

Note: avoid returning ​null​ values when you can. Don't feed a variable in the dataLayer if it has no value.

<script>
window.dataLayer.push({
ga4MeasurementId: 'G-XXXXXXXX',
gaUniversalTrackingId: null // Bad
});
</script>

In order to use our containers, certain variables need to be defined in a standard way.

Analytics

Google Analytics

These are applicable to most sites.

PropertyDescriptionType
ga4MeasurementIdE.g. G- ID. Found within the GA interface in Admin > Data Streams > AllString
gaUniversalTrackingIdE.g. UA- ID. Found within the GA interface in Admin > Property Settings > Tracking IdString

Social Media Tracking

PropertyDescriptionType
twitterIdTwitter Pixel ID found in your Twitter Ads accountString

Advertising

Research Articles

PropertyDescriptionType
content.article.doi...String
content.authorization.status...String
content.category.backHalfContent...String
content.category.backHalfContent...String
content.category.contentType...String
content.contentInfo.title...String
page.category.pageType...String
page.category.pageType...String
user.profile.profileInfo.bpid...String
user.profile.profileInfo.resolvedBy...String