Tracking | What is a data layer?
Introduction
The data layer is a variable "object" created by the script that installs Google Tag Manager (GTM) on a website. The data layer facilitates communication of data directly from web to GTM and by extension Google Analytics (GA). Using data layer for GA tracking is considered best practice because it is more accurate, and maintainable than other methods of tracking user interactions.
Because Google Tag Manager is a prerequisite, this guide assumes you already have GTM installed on the pages you wish to track.
Google Documentation - Data Layer
Examples
This section contains two subsections: one for lead generation tracking, and one for ecommerce.
Note: depending on your reporting requirements we may advise you on pushing custom data in addition to what is demonstrated below.
Lead Generation
We recommend executing the following data layer push following any web interaction that should be tracked as a conversion.
window.dataLayer.push({'event': 'ChannelMix.conversion'});
Ecommerce
The purchase event demonstrated supports the minimum required data for tracking the purchase of a product called 'Widget Pro' that costs $99.99 USD. These values should be dynamically set based on the products(s) a customer is purchasing for any given transaction.
window.dataLayer.push({
'event': 'purchase',
'currency': 'USD',
'transaction_id': 'T_12345',
'value': '99.99',
'items': {
'item_id': 'SKU_12345',
'item_name': 'Widget Pro'
}
})