Shila Mosammami
Shila Mosammami

Reputation: 1077

Why datalayer.push does not send info to Google Analytics Data Api (GA4)?

I am trying to test if I can do the same thing I have done with gtag in GA4 using datalayer.push. Everything works well with gtag but not in datalayer.push. In code below, I have defined two custom events : gTracker which works well and dlTracker which is only visible in tagassistant and does not send the info to Google Analytics Data API.

in HTML I have

  <button  type='button' class='btn btn-primary' onclick="javascript:BTN_click()">Click Me</button>  

in BTN_click() ==>

   <script>
    function BTN_click() {
      var url = "<?php echo $url; ?>";
      window.dataLayer = window.dataLayer || [];
      dataLayer.push({
        'event': 'dlTracker',
        'storeId': <?php echo $store; ?>
      });

      gtag('event', 'gTracker', {
        'storeId': <?php echo $store; ?>
      });
      window.open(url, '_blank');
    }
  </script>

Immediately after head tag in HTML :

   <!-- Google tag (gtag.js) -->
  <script async src="https://www.googletagmanager.com/gtag/js?id=G-XXXXXX"></script>
  <script>
    window.dataLayer = window.dataLayer || [];

    function gtag() {
      dataLayer.push(arguments);
    }
    gtag('js', new Date());

    gtag('config', 'G-XXXXXXX');
  </script>


  <!-- Google Tag Manager 
   Google Tag Manager container’s JavaScript Snippet-->
  <script>
    (function(w, d, s, l, i) {
      w[l] = w[l] || [];
      w[l].push({
        'gtm.start': new Date().getTime(),
        event: 'gtm.js'
      });
      var f = d.getElementsByTagName(s)[0],
        j = d.createElement(s),
        dl = l != 'dataLayer' ? '&l=' + l : '';
      j.async = true;
      j.src =
        'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
      f.parentNode.insertBefore(j, f);
    })(window, document, 'script', 'dataLayer', 'GTM-XXXXX');
  </script>
  <!-- End Google Tag Manager -->

The custom dimension storeId has been created in Google Analytics account. If I use just gtag() it works and I do not problem. However using datalayer, I have even defined tag and triggers in GTM(Google Tag Manager).

In Debug View I can only see the event created by gtag. But in tagassistant I see both events. But, it is not sending the info related to dlTracker event to GA4 API.

Thank you.

Upvotes: 2

Views: 3277

Answers (1)

Shila Mosammami
Shila Mosammami

Reputation: 1077

ok guys, thanks to Julius Fedorovicius I found out if we wanna use dataLayer.push directly, then we need to create the variables we want and make it as parameters to pass in the tag we have created in tagmanager. Open variables tab in tagmanager then at the bottom of the page select inUser-Defined Variablessection create a new variable. Then, in Variable Configuration select Data Layer Variable and for the Data Layer Variable Name write the name of your variable that you have written in dataLayer.push in coding part, which in my case is storeId. pay attention to spelling!. Type a name for this variable at the top left part and click save button (I chose DLV_storeId).

Next, open your tag you have created for your custom event in edit mode, then click on Event Parameters and then select Add Parameter, after that, in the box for Parameter Name: enter the name which in my case is storeId. and in value section click on + button and try to find what you have just created. finally submit and publish the changes.

I do not know why I cannot upload a screenshot, it gives server error! in case anyone needs more visual description, you can find watch the free tutorial at link above.

Upvotes: 2

Related Questions