How to use GTM (Google Tag Manager) DataLayer Variables

Created by LiRou C, Modified on Fri, 1 Nov at 1:20 AM by Donald Ng

8-min video demo




In this guide, you will learn how to use Google Tag Manager's DataLayer Variables to send custom event attributes (such as revenue, quantity, and currency) from your GTM setup to Mida for A/B testing and reporting purposes.


Prerequisites

You should have a purchase event set up in Google Tag Manager (GTM) with attributes like revenue, quantity, and currency already defined in the DataLayer.


Steps to Set Up DataLayer Variables in GTM


1. Prepare Your DataLayer

Ensure that your DataLayer contains the necessary attributes for your purchase event. These attributes could have different names, such as:

  • sales_total for revenue
  • sales_quantity for quantity
  • sales_currency for currency


For example, your purchase event could look like this:

window.dataLayer.push({
  event: "purchase_event",
  sales_total: 5555,
  sales_quantity: 15,
  sales_currency: "EUR"
});


2. Create a Trigger for the Purchase Event

If you haven’t set up a trigger for your purchase event in GTM, follow these steps:

  • Navigate to Triggers in GTM.
  • Click New and name it Purchase Event.
  • Under Trigger Configuration, choose Custom Event and enter the event name, e.g., purchase_event.
  • Click Save.


3. Create DataLayer Variables

Now, you'll create DataLayer variables to capture the necessary event attributes (sales total, sales quantity, and sales currency). Follow these steps for each attribute:

  1. Create Variable for Sales Total
    • Navigate to Variables in GTM.
    • Click New under the User-Defined Variables section.
    • Name it Purchase Revenue.
    • Under Variable Configuration, choose Data Layer Variable.
    • In the Data Layer Variable Name field, enter sales_total.
    • Save the variable.
  2. Create Variable for Sales Quantity
    • Repeat the above steps, but name the variable Purchase Quantity.
    • In the Data Layer Variable Name field, enter sales_quantity.
    • Save the variable.
  3. Create Variable for Sales Currency
    • Repeat the above steps, but name the variable Purchase Currency.
    • In the Data Layer Variable Name field, enter sales_currency.
    • Save the variable.


4. Create a Tag to Send the Event to Mida

Next, create a tag to send the purchase event along with the attributes to Mida for A/B testing reporting.

  1. Configure the Tag

    • Navigate to Tags in GTM and click New.
    • Name the tag Send Purchase Event to Mida.
    • Under Tag Configuration, choose Custom HTML.
    • In the Custom HTML field, paste the Mida event snippet provided by Mida (modify the snippet to reference the DataLayer variables you've created).
  2. For example:

    <script>
    mida.track('Purchase', {
      revenue: {{Purchase Revenue}},
      quantity: {{Purchase Quantity}},
      currency: "{{Purchase Currency}}"
    });
    </script>
    
  3. Set the trigger to the Purchase Event trigger you created earlier.

  4. Save the Tag by clicking Save to finish creating the tag.


5. Test and Verify the Setup

Once you’ve created the tag, test the setup using GTM’s Preview Mode:

  1. Preview Mode: Enable Preview mode in GTM and test your website’s purchase event to ensure that the purchase event is firing correctly with the variables you’ve set.
  2. Console Log: Optionally, add a console log in the Custom HTML tag to verify that the correct data is being passed, e.g.:
    <script>
    console.log('Sending event to Mida:', {
      revenue: {{Purchase Revenue}},
      quantity: {{Purchase Quantity}},
      currency: "{{Purchase Currency}}"
     });
    </script>
    
  1. Check Network Requests: After triggering a purchase event, inspect the Network tab in your browser’s Developer Tools to verify that the event is successfully sent to Mida. Look for the network request with the event data, ensuring that the values for revenue, quantity, and currency are correct.


6. Publish Your Changes

Once everything is working as expected, go back to GTM and Submit your changes to publish the tag, trigger, and variables.


With these steps, you’ve successfully set up Google Tag Manager to capture purchase event attributes using DataLayer variables and send them to Mida for A/B testing and reporting purposes.


This allows you to track and analyze key metrics such as revenue, quantity, and currency in your A/B test results.

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article