About a data layer

A data layer is a data structure on your site or app where you can store data and access it with tools like Tag Manager. You can include any data you want in your data layer. Here are some examples:

  • Page information: category, region, currency 
  • Product information: product name, category, price
  • Order information: order ID, total, shipping
  • User information: user ID, new / returning user, city 
  • Event information: event name, label of a clicked button

Tag Manager can read data from your data layer and use it for tags, triggers and variables, just like it would use data from page’s source code.

Add a data layer to your site or app

To add a data layer to your site, follow these steps:

  1. Add information to the data layer with the dataLayer.push() command. Example:
    <script>
        window.dataLayer = window.dataLayer || [];
        window.dataLayer.push({
            'blogCategory': 'Home budget',
            'blogAuthor': 'John Doe',
        });
    </script>

    This command window.dataLayer = window.dataLayer || []; creates a data layer. The dataLayer.push() command adds information in key-value pairs. In our example, the key-value pairs are:

    • 'blogCategory': 'Home budget'
    • 'blogAuthor': 'John Doe'

    The key is a category of things like a blog category or blog author. Each key can have different values. For example, a blog category key can have values like 'Home budget', 'Savings', ‘Investments’ and the like.

    Note:

    • Information in the data layer is assigned to each page so you need to make sure all pages hold the data you need.
    • If you want to use information from the data layer before the page loads, add the data layer code above the container’s code.
    • The default data layer name is dataLayer, but you can change it to a custom one. Read more

Set up a data layer variable

When you have your keys ready, you can access it with a data layer variable.

To set up a data layer variable, follow these steps:

  1. Go to Menu > Tag Manager.
  2. Navigate to Variables.
  3. Click Add a variable.
  4. Name the variable. Example: Blog category.
  5. Select the following variable type: Data layer.
  6. In Data layer variable name, type the key exactly as it is written in the code. In our example, it’s blogCategory.
    Data layer in Piwik PRO
  7. Click OK.
  8. Repeat these steps for every data layer key that you want to use as a variable in Tag Manager.
  9. Test your variable in debug mode.
  10. When you’re happy with how the variable works, click Publish.

Add an event to a data layer

So far we’ve shown you how to use a data layer to add information in the form of key-value pairs. But you can also use a data layer to add a new event and then use it in a trigger.

To add an event to a data layer, follow these steps:

  1. Add information to the data layer with the dataLayer.push() command. Example:
    <script>
        window.dataLayer = window.dataLayer || [];
        window.dataLayer.push({
            'event': 'event_name',
        });
    </script>

    or

    <script>
        window.dataLayer = window.dataLayer || [];
        window.dataLayer.push({
            'event': 'event_name',
            'event_key': 'event_value',
        });
    </script>

    This command event: 'event_name' holds information about the event name. This command event: 'event_name', 'event_key': 'event_value' can additionally hold information about the event key and value. You can access the key and value using a variable.

Set up a data layer event trigger

When you have your events in a data layer ready, you can use it in a data layer event trigger.

To set up a data layer event trigger, follow these steps:

  1. Go to Menu > Tag Manager.
  2. Navigate to Triggers.
  3. Click Add a trigger.
  4. Name the trigger.
  5. In Event type, choose the following type: Data layer event.
    Data layer in Piwik PRO
  6. Click Next.
  7. Enter the event name that you set in your data layer.
    Data layer in Piwik PRO
  8. Optionally, define event conditions.
  9. Click OK.
  10. Test your variable in debug mode.
  11. If everything works correctly, click Publish.

Examples

Here are some examples of information or events pushed to a data layer:

  • Send an event when a visitor clicks on a button
    <button onclick="dataLayer.push({'event': sign_up});">Sign up</button>
  • If your forms have a custom validation in JavaScript, you can send an event when a visitor submits or fails to submit a form. Such events can then be used in Tag Manager.
    dataLayer.push({'event': 'form_submitted_successfully'}) 
    dataLayer.push({'event': 'form_validation_error'})

Troubleshooting

Here are some tips on how to work with the data layer:

  • Don’t overwrite the window.dataLayer variable: Don’t use the data layer directly dataLayer = [{'item': 'value'}]) because it will overwrite all existing values in the data layer. Always declare the data layer with window.dataLayer = window.dataLayer || []; and then use dataLayer.push({'item': 'value'}) to add information in key-value pairs.
  • The dataLayer object name is case-sensitive: Always write dataLayer with correct casing (camel case). Example:
    datalayer.push({'blogCategory': 'Home budget'});    // Bad (lowercase: datalayer)
    dataLayer.push({'blogCategory': 'Home budget'});    // Good (camel case: dataLayer)
  • Use quotes for variable names with special characters: Example:
    dataLayer.push({blog-category: 'Home budget'});      // Bad (no quote marks)
    dataLayer.push({'blog-category': 'Home budget'});    // Good (quote marks)
  • Keep variable names consistent across pages: When you use the same variable across pages, keep its name consistent.

    Bad

    // Homepage:
    dataLayer.push({'blogCategory': 'Home budget'}); 
    
    // Blog post page:
    dataLayer.push({'blog-category': 'Home budget'}); 

    Good

    // Homepage:
    dataLayer.push({'blogCategory': 'Home budget'}); 
    
    // Blog post page:
    dataLayer.push({'blogCategory': 'Home budget'}); 

Was this article helpful?

Technical support

If you still have some questions, visit our community.
There’s always someone ready to help!

Related articles

Back to help center