How to organize ABn testing through Tag Manager

Step 1: Create your own events in Google Analytics

Create a goal with your own event

Step 2: Create a sending code

The code to send information about the triggered event to Google Analytics looks like this:

_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', 'opt_value', 'opt_noninteraction']);
onclick
<button id="my_btn" onclick="_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', 'opt_value', 'opt_noninteraction']);">My Button</button>
var TransferBtn = document.getElementById("transfer");
TransferBtn.onclick = (function() {
_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', 'opt_value', 'opt_noninteraction'])
})();
  1. category
  2. action
  3. opt_label
  4. opt_value

Step 3: How to send data to GA through the dataLayer

Go to the “Variables” section in GTM, and in the “User Variables” field create 4 dataLayer variables there (dataLayer). I have them: eventCategory, eventAction, eventLabel, eventValue.

  1. Go to the “Triggers” section and create a trigger with the “Custom event” type there.
  2. In the name of the event we write any name. This name will be the activation condition for our script, which will send data through the dataLayer.
  1. Use as category, action, label, and value our previously created custom variables: eventCategory, eventAction, eventLabel, eventValue.
  2. Activate the event by the trigger we created (user event) — event-to-ga.

Step 4: Complete the listening code and publish it in the Google Tag Manager container

Almost everything is ready. It remains to add the code to the event listening script to send about the triggered event.

var TransferBtn = document.getElementById("transfer");
TransferBtn.onclick = (function() {
_gaq.push(['_trackEvent', 'category', 'action', 'opt_label', 'opt_value', 'opt_noninteraction'])
})();
dataLayer.push({‘event’: ‘event-to-ga‘, ‘eventCategory’ : ‘category_value’, ‘eventAction’ : ‘action_value’, ‘eventLabel’ : ‘label_value’, ‘eventValue’ : ‘value_value’ });

Step 5: Checking that the code works

We publish our container. And then right away we need to check if everything is working properly. So we go to Google Analytics and open the Real-time section, subsection Events. If the traffic on your site is high enough, the window that opens should have numbers greater than zero, and floating charts in the “by seconds” block.

How to organize ABn testing through Tag Manager

--

--

UI designer who codes on a freelance basis. I’m on my way to become a UX (Product Designer) in a large company.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
azamat.design/

azamat.design/

UI designer who codes on a freelance basis. I’m on my way to become a UX (Product Designer) in a large company.