Make sure Google Tag Manager and Google Analytics is done before redirect











up vote
4
down vote

favorite
1












I have a link to my webpage where I want the user to be tracked using Google Analytics and Google Tag Manager before I redirect the user to an external page.



I want to make sure GA and GTM are done before I do the redirect. What's the best approach?



A simple approach is using a setTimeout. But is 1000ms too long or too short? Would prefer if it was possible to do redirect when tracking is actually finished?. Is GTM and GA tracking done before document ready? If not can this be forced?



<html lang="sv">
<head>
<title></title>
<meta charset="utf-8"/>
<script src="~/Scripts/jquery-2.1.0.min.js"></script>
<!-- ANALYTICS CODE -->
</head>
<body>

<!-- TAG MANAGER CODE -->

<script language="javascript">
$(function() {
setTimeout(function() {
window.location.replace("http://externalwebsite.com");
},1000);
});
</script>

</body>
</html>


In the GTM bucket I have Twitter and a Facebook remarketing tag.



UPDATED SOLUTION



I've updated my solution. Since I'm mostly interested in making sure GA absolutely fires before redirect this will work better.



<html lang="sv">
<head>
<title></title>
<meta charset="utf-8"/>
<script src="~/Scripts/jquery-2.1.0.min.js"></script>
<script language="javascript">

var redirect = function(waitFor) {
return function(signal) {
waitFor[signal] = 1;
for (var s in waitFor) if (waitFor[s] == 0) return;
_redirect();
}
}({timeout:0,ga:0});

var redirected = false;
var _redirect = function () {
if (!redirected) {
redirected = true;
window.location.replace("http://@Model");
}
};

(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
(i[r].q = i[r].q || ).push(arguments)
}, i[r].l = 1 * new Date(); a = s.createElement(o),
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-47608023-1', 'tessin.se');
ga('require', 'displayfeatures');
ga('send', 'pageview', {
'hitCallback': function () {
redirect("ga");
}
});
</script>

</head>
<body>

@Html.Include("google-tagmanager-redirect.html")

<script language="javascript">
$(function () {
setTimeout(function () { redirect("timeout"); }, 1000);
setTimeout(function () { _redirect(); }, 3000);
});
</script>

</body>
</html>









share|improve this question




















  • 1




    I don't know about GTM. But for GA, you can refer to stackoverflow.com/a/23766246/413337. It uses the hitCallback option to specify a function that is called when the Google Analytics event has been successfully sent.
    – Codo
    Sep 7 '15 at 13:11










  • Wouldn't it be better to use transport=beacon field in GA? It should guarantee delivery
    – Dmitry
    Jul 19 at 4:06















up vote
4
down vote

favorite
1












I have a link to my webpage where I want the user to be tracked using Google Analytics and Google Tag Manager before I redirect the user to an external page.



I want to make sure GA and GTM are done before I do the redirect. What's the best approach?



A simple approach is using a setTimeout. But is 1000ms too long or too short? Would prefer if it was possible to do redirect when tracking is actually finished?. Is GTM and GA tracking done before document ready? If not can this be forced?



<html lang="sv">
<head>
<title></title>
<meta charset="utf-8"/>
<script src="~/Scripts/jquery-2.1.0.min.js"></script>
<!-- ANALYTICS CODE -->
</head>
<body>

<!-- TAG MANAGER CODE -->

<script language="javascript">
$(function() {
setTimeout(function() {
window.location.replace("http://externalwebsite.com");
},1000);
});
</script>

</body>
</html>


In the GTM bucket I have Twitter and a Facebook remarketing tag.



UPDATED SOLUTION



I've updated my solution. Since I'm mostly interested in making sure GA absolutely fires before redirect this will work better.



<html lang="sv">
<head>
<title></title>
<meta charset="utf-8"/>
<script src="~/Scripts/jquery-2.1.0.min.js"></script>
<script language="javascript">

var redirect = function(waitFor) {
return function(signal) {
waitFor[signal] = 1;
for (var s in waitFor) if (waitFor[s] == 0) return;
_redirect();
}
}({timeout:0,ga:0});

var redirected = false;
var _redirect = function () {
if (!redirected) {
redirected = true;
window.location.replace("http://@Model");
}
};

(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
(i[r].q = i[r].q || ).push(arguments)
}, i[r].l = 1 * new Date(); a = s.createElement(o),
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-47608023-1', 'tessin.se');
ga('require', 'displayfeatures');
ga('send', 'pageview', {
'hitCallback': function () {
redirect("ga");
}
});
</script>

</head>
<body>

@Html.Include("google-tagmanager-redirect.html")

<script language="javascript">
$(function () {
setTimeout(function () { redirect("timeout"); }, 1000);
setTimeout(function () { _redirect(); }, 3000);
});
</script>

</body>
</html>









share|improve this question




















  • 1




    I don't know about GTM. But for GA, you can refer to stackoverflow.com/a/23766246/413337. It uses the hitCallback option to specify a function that is called when the Google Analytics event has been successfully sent.
    – Codo
    Sep 7 '15 at 13:11










  • Wouldn't it be better to use transport=beacon field in GA? It should guarantee delivery
    – Dmitry
    Jul 19 at 4:06













up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





I have a link to my webpage where I want the user to be tracked using Google Analytics and Google Tag Manager before I redirect the user to an external page.



I want to make sure GA and GTM are done before I do the redirect. What's the best approach?



A simple approach is using a setTimeout. But is 1000ms too long or too short? Would prefer if it was possible to do redirect when tracking is actually finished?. Is GTM and GA tracking done before document ready? If not can this be forced?



<html lang="sv">
<head>
<title></title>
<meta charset="utf-8"/>
<script src="~/Scripts/jquery-2.1.0.min.js"></script>
<!-- ANALYTICS CODE -->
</head>
<body>

<!-- TAG MANAGER CODE -->

<script language="javascript">
$(function() {
setTimeout(function() {
window.location.replace("http://externalwebsite.com");
},1000);
});
</script>

</body>
</html>


In the GTM bucket I have Twitter and a Facebook remarketing tag.



UPDATED SOLUTION



I've updated my solution. Since I'm mostly interested in making sure GA absolutely fires before redirect this will work better.



<html lang="sv">
<head>
<title></title>
<meta charset="utf-8"/>
<script src="~/Scripts/jquery-2.1.0.min.js"></script>
<script language="javascript">

var redirect = function(waitFor) {
return function(signal) {
waitFor[signal] = 1;
for (var s in waitFor) if (waitFor[s] == 0) return;
_redirect();
}
}({timeout:0,ga:0});

var redirected = false;
var _redirect = function () {
if (!redirected) {
redirected = true;
window.location.replace("http://@Model");
}
};

(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
(i[r].q = i[r].q || ).push(arguments)
}, i[r].l = 1 * new Date(); a = s.createElement(o),
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-47608023-1', 'tessin.se');
ga('require', 'displayfeatures');
ga('send', 'pageview', {
'hitCallback': function () {
redirect("ga");
}
});
</script>

</head>
<body>

@Html.Include("google-tagmanager-redirect.html")

<script language="javascript">
$(function () {
setTimeout(function () { redirect("timeout"); }, 1000);
setTimeout(function () { _redirect(); }, 3000);
});
</script>

</body>
</html>









share|improve this question















I have a link to my webpage where I want the user to be tracked using Google Analytics and Google Tag Manager before I redirect the user to an external page.



I want to make sure GA and GTM are done before I do the redirect. What's the best approach?



A simple approach is using a setTimeout. But is 1000ms too long or too short? Would prefer if it was possible to do redirect when tracking is actually finished?. Is GTM and GA tracking done before document ready? If not can this be forced?



<html lang="sv">
<head>
<title></title>
<meta charset="utf-8"/>
<script src="~/Scripts/jquery-2.1.0.min.js"></script>
<!-- ANALYTICS CODE -->
</head>
<body>

<!-- TAG MANAGER CODE -->

<script language="javascript">
$(function() {
setTimeout(function() {
window.location.replace("http://externalwebsite.com");
},1000);
});
</script>

</body>
</html>


In the GTM bucket I have Twitter and a Facebook remarketing tag.



UPDATED SOLUTION



I've updated my solution. Since I'm mostly interested in making sure GA absolutely fires before redirect this will work better.



<html lang="sv">
<head>
<title></title>
<meta charset="utf-8"/>
<script src="~/Scripts/jquery-2.1.0.min.js"></script>
<script language="javascript">

var redirect = function(waitFor) {
return function(signal) {
waitFor[signal] = 1;
for (var s in waitFor) if (waitFor[s] == 0) return;
_redirect();
}
}({timeout:0,ga:0});

var redirected = false;
var _redirect = function () {
if (!redirected) {
redirected = true;
window.location.replace("http://@Model");
}
};

(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
(i[r].q = i[r].q || ).push(arguments)
}, i[r].l = 1 * new Date(); a = s.createElement(o),
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-47608023-1', 'tessin.se');
ga('require', 'displayfeatures');
ga('send', 'pageview', {
'hitCallback': function () {
redirect("ga");
}
});
</script>

</head>
<body>

@Html.Include("google-tagmanager-redirect.html")

<script language="javascript">
$(function () {
setTimeout(function () { redirect("timeout"); }, 1000);
setTimeout(function () { _redirect(); }, 3000);
});
</script>

</body>
</html>






javascript google-analytics google-tag-manager






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 7 '15 at 13:34

























asked Sep 7 '15 at 12:13









Niels Bosma

4,3892581137




4,3892581137








  • 1




    I don't know about GTM. But for GA, you can refer to stackoverflow.com/a/23766246/413337. It uses the hitCallback option to specify a function that is called when the Google Analytics event has been successfully sent.
    – Codo
    Sep 7 '15 at 13:11










  • Wouldn't it be better to use transport=beacon field in GA? It should guarantee delivery
    – Dmitry
    Jul 19 at 4:06














  • 1




    I don't know about GTM. But for GA, you can refer to stackoverflow.com/a/23766246/413337. It uses the hitCallback option to specify a function that is called when the Google Analytics event has been successfully sent.
    – Codo
    Sep 7 '15 at 13:11










  • Wouldn't it be better to use transport=beacon field in GA? It should guarantee delivery
    – Dmitry
    Jul 19 at 4:06








1




1




I don't know about GTM. But for GA, you can refer to stackoverflow.com/a/23766246/413337. It uses the hitCallback option to specify a function that is called when the Google Analytics event has been successfully sent.
– Codo
Sep 7 '15 at 13:11




I don't know about GTM. But for GA, you can refer to stackoverflow.com/a/23766246/413337. It uses the hitCallback option to specify a function that is called when the Google Analytics event has been successfully sent.
– Codo
Sep 7 '15 at 13:11












Wouldn't it be better to use transport=beacon field in GA? It should guarantee delivery
– Dmitry
Jul 19 at 4:06




Wouldn't it be better to use transport=beacon field in GA? It should guarantee delivery
– Dmitry
Jul 19 at 4:06












4 Answers
4






active

oldest

votes

















up vote
4
down vote













The easiest way is probably to do the redirection from within GTM.



Create a custom HTML tag with the redirection code. Then use tag sequencing to trigger the redirection tag, that way you make sure GA is triggered before the redirect.






share|improve this answer





















  • Cool. Any source on this?
    – Niels Bosma
    Sep 7 '15 at 13:28






  • 1




    I linked the docs for tag sequencing and an article by Simon Ahava (he's pretty much the go-to guy for anything GTM related). Tag sequencing is a rather new feature, I don't think there is much more info.
    – Eike Pierstorff
    Sep 7 '15 at 13:33










  • Ok, missed that it was two links. This makes GTM too complicated to be useful in my case. Easier to move Twitter and Facebook tags to the page.
    – Niels Bosma
    Sep 7 '15 at 14:15


















up vote
1
down vote













One possible solution is to set the 'transport' flag, either directly in your GA code or by setting the field in GTM.



More info about that can be found here: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#transport. Note that GTM v2 still allows for the 'useBeacon' flag, but it is deprecated and if you code directly in GA, then you should use 'transport' instead.



For example, if directly coding in this flag:



ga('send', 'event', 'click', 'download-me', {transport: 'beacon'});


or in GTM:



enter image description here



This flag ensures that the hit is sent before the page navigates away.






share|improve this answer





















  • Could you explain "before the page navigates away"? Would this beacon:true tag block the redirection code until it loads completely even if the redirect code is separately written at the bottom of </body> tag on the page?
    – Aditya Bajaj
    Mar 23 at 17:13




















up vote
1
down vote













When pushing to dataLayer with GTM you can use the eventCallback property.



dataLayer.push({
'key1' : 'value1',
'key2' : 'value2',
'event' : 'fireTags',
'eventCallback' : function() {
alert('ALL tags which fire on {{event}} equals fireTags have now fired');
}
});


More info: https://www.simoahava.com/gtm-tips/hitcallback-eventcallback/






share|improve this answer




























    up vote
    0
    down vote













    In reference to Eike's answer. I created the following workflow to get this working;



    In my code I add to the dataLayer



    <script type="text/javascript">
    dataLayer.push({
    'event': 'gtm_redirect_url',
    'gtm_redirect_url': '<?php echo $url; ?>'
    });
    </script>


    In this case I'm using PHP to change the redirect url based on certain conditions.



    Then in GTM I set up a variable called gtm_redirect_url



    enter image description here



    And a trigger also called gtm_redirect_url;



    enter image description here



    And a tag to fire the JS in an HTML snippet



    enter image description here



    All pretty simple to set up and does ensure GTM has loaded before the redirect. This method also makes it easy to add the redirect data layer tag to any page you wish.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32438499%2fmake-sure-google-tag-manager-and-google-analytics-is-done-before-redirect%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote













      The easiest way is probably to do the redirection from within GTM.



      Create a custom HTML tag with the redirection code. Then use tag sequencing to trigger the redirection tag, that way you make sure GA is triggered before the redirect.






      share|improve this answer





















      • Cool. Any source on this?
        – Niels Bosma
        Sep 7 '15 at 13:28






      • 1




        I linked the docs for tag sequencing and an article by Simon Ahava (he's pretty much the go-to guy for anything GTM related). Tag sequencing is a rather new feature, I don't think there is much more info.
        – Eike Pierstorff
        Sep 7 '15 at 13:33










      • Ok, missed that it was two links. This makes GTM too complicated to be useful in my case. Easier to move Twitter and Facebook tags to the page.
        – Niels Bosma
        Sep 7 '15 at 14:15















      up vote
      4
      down vote













      The easiest way is probably to do the redirection from within GTM.



      Create a custom HTML tag with the redirection code. Then use tag sequencing to trigger the redirection tag, that way you make sure GA is triggered before the redirect.






      share|improve this answer





















      • Cool. Any source on this?
        – Niels Bosma
        Sep 7 '15 at 13:28






      • 1




        I linked the docs for tag sequencing and an article by Simon Ahava (he's pretty much the go-to guy for anything GTM related). Tag sequencing is a rather new feature, I don't think there is much more info.
        – Eike Pierstorff
        Sep 7 '15 at 13:33










      • Ok, missed that it was two links. This makes GTM too complicated to be useful in my case. Easier to move Twitter and Facebook tags to the page.
        – Niels Bosma
        Sep 7 '15 at 14:15













      up vote
      4
      down vote










      up vote
      4
      down vote









      The easiest way is probably to do the redirection from within GTM.



      Create a custom HTML tag with the redirection code. Then use tag sequencing to trigger the redirection tag, that way you make sure GA is triggered before the redirect.






      share|improve this answer












      The easiest way is probably to do the redirection from within GTM.



      Create a custom HTML tag with the redirection code. Then use tag sequencing to trigger the redirection tag, that way you make sure GA is triggered before the redirect.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Sep 7 '15 at 13:07









      Eike Pierstorff

      24.4k32345




      24.4k32345












      • Cool. Any source on this?
        – Niels Bosma
        Sep 7 '15 at 13:28






      • 1




        I linked the docs for tag sequencing and an article by Simon Ahava (he's pretty much the go-to guy for anything GTM related). Tag sequencing is a rather new feature, I don't think there is much more info.
        – Eike Pierstorff
        Sep 7 '15 at 13:33










      • Ok, missed that it was two links. This makes GTM too complicated to be useful in my case. Easier to move Twitter and Facebook tags to the page.
        – Niels Bosma
        Sep 7 '15 at 14:15


















      • Cool. Any source on this?
        – Niels Bosma
        Sep 7 '15 at 13:28






      • 1




        I linked the docs for tag sequencing and an article by Simon Ahava (he's pretty much the go-to guy for anything GTM related). Tag sequencing is a rather new feature, I don't think there is much more info.
        – Eike Pierstorff
        Sep 7 '15 at 13:33










      • Ok, missed that it was two links. This makes GTM too complicated to be useful in my case. Easier to move Twitter and Facebook tags to the page.
        – Niels Bosma
        Sep 7 '15 at 14:15
















      Cool. Any source on this?
      – Niels Bosma
      Sep 7 '15 at 13:28




      Cool. Any source on this?
      – Niels Bosma
      Sep 7 '15 at 13:28




      1




      1




      I linked the docs for tag sequencing and an article by Simon Ahava (he's pretty much the go-to guy for anything GTM related). Tag sequencing is a rather new feature, I don't think there is much more info.
      – Eike Pierstorff
      Sep 7 '15 at 13:33




      I linked the docs for tag sequencing and an article by Simon Ahava (he's pretty much the go-to guy for anything GTM related). Tag sequencing is a rather new feature, I don't think there is much more info.
      – Eike Pierstorff
      Sep 7 '15 at 13:33












      Ok, missed that it was two links. This makes GTM too complicated to be useful in my case. Easier to move Twitter and Facebook tags to the page.
      – Niels Bosma
      Sep 7 '15 at 14:15




      Ok, missed that it was two links. This makes GTM too complicated to be useful in my case. Easier to move Twitter and Facebook tags to the page.
      – Niels Bosma
      Sep 7 '15 at 14:15












      up vote
      1
      down vote













      One possible solution is to set the 'transport' flag, either directly in your GA code or by setting the field in GTM.



      More info about that can be found here: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#transport. Note that GTM v2 still allows for the 'useBeacon' flag, but it is deprecated and if you code directly in GA, then you should use 'transport' instead.



      For example, if directly coding in this flag:



      ga('send', 'event', 'click', 'download-me', {transport: 'beacon'});


      or in GTM:



      enter image description here



      This flag ensures that the hit is sent before the page navigates away.






      share|improve this answer





















      • Could you explain "before the page navigates away"? Would this beacon:true tag block the redirection code until it loads completely even if the redirect code is separately written at the bottom of </body> tag on the page?
        – Aditya Bajaj
        Mar 23 at 17:13

















      up vote
      1
      down vote













      One possible solution is to set the 'transport' flag, either directly in your GA code or by setting the field in GTM.



      More info about that can be found here: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#transport. Note that GTM v2 still allows for the 'useBeacon' flag, but it is deprecated and if you code directly in GA, then you should use 'transport' instead.



      For example, if directly coding in this flag:



      ga('send', 'event', 'click', 'download-me', {transport: 'beacon'});


      or in GTM:



      enter image description here



      This flag ensures that the hit is sent before the page navigates away.






      share|improve this answer





















      • Could you explain "before the page navigates away"? Would this beacon:true tag block the redirection code until it loads completely even if the redirect code is separately written at the bottom of </body> tag on the page?
        – Aditya Bajaj
        Mar 23 at 17:13















      up vote
      1
      down vote










      up vote
      1
      down vote









      One possible solution is to set the 'transport' flag, either directly in your GA code or by setting the field in GTM.



      More info about that can be found here: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#transport. Note that GTM v2 still allows for the 'useBeacon' flag, but it is deprecated and if you code directly in GA, then you should use 'transport' instead.



      For example, if directly coding in this flag:



      ga('send', 'event', 'click', 'download-me', {transport: 'beacon'});


      or in GTM:



      enter image description here



      This flag ensures that the hit is sent before the page navigates away.






      share|improve this answer












      One possible solution is to set the 'transport' flag, either directly in your GA code or by setting the field in GTM.



      More info about that can be found here: https://developers.google.com/analytics/devguides/collection/analyticsjs/field-reference#transport. Note that GTM v2 still allows for the 'useBeacon' flag, but it is deprecated and if you code directly in GA, then you should use 'transport' instead.



      For example, if directly coding in this flag:



      ga('send', 'event', 'click', 'download-me', {transport: 'beacon'});


      or in GTM:



      enter image description here



      This flag ensures that the hit is sent before the page navigates away.







      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Sep 23 '15 at 14:00









      nyuen

      7,53621024




      7,53621024












      • Could you explain "before the page navigates away"? Would this beacon:true tag block the redirection code until it loads completely even if the redirect code is separately written at the bottom of </body> tag on the page?
        – Aditya Bajaj
        Mar 23 at 17:13




















      • Could you explain "before the page navigates away"? Would this beacon:true tag block the redirection code until it loads completely even if the redirect code is separately written at the bottom of </body> tag on the page?
        – Aditya Bajaj
        Mar 23 at 17:13


















      Could you explain "before the page navigates away"? Would this beacon:true tag block the redirection code until it loads completely even if the redirect code is separately written at the bottom of </body> tag on the page?
      – Aditya Bajaj
      Mar 23 at 17:13






      Could you explain "before the page navigates away"? Would this beacon:true tag block the redirection code until it loads completely even if the redirect code is separately written at the bottom of </body> tag on the page?
      – Aditya Bajaj
      Mar 23 at 17:13












      up vote
      1
      down vote













      When pushing to dataLayer with GTM you can use the eventCallback property.



      dataLayer.push({
      'key1' : 'value1',
      'key2' : 'value2',
      'event' : 'fireTags',
      'eventCallback' : function() {
      alert('ALL tags which fire on {{event}} equals fireTags have now fired');
      }
      });


      More info: https://www.simoahava.com/gtm-tips/hitcallback-eventcallback/






      share|improve this answer

























        up vote
        1
        down vote













        When pushing to dataLayer with GTM you can use the eventCallback property.



        dataLayer.push({
        'key1' : 'value1',
        'key2' : 'value2',
        'event' : 'fireTags',
        'eventCallback' : function() {
        alert('ALL tags which fire on {{event}} equals fireTags have now fired');
        }
        });


        More info: https://www.simoahava.com/gtm-tips/hitcallback-eventcallback/






        share|improve this answer























          up vote
          1
          down vote










          up vote
          1
          down vote









          When pushing to dataLayer with GTM you can use the eventCallback property.



          dataLayer.push({
          'key1' : 'value1',
          'key2' : 'value2',
          'event' : 'fireTags',
          'eventCallback' : function() {
          alert('ALL tags which fire on {{event}} equals fireTags have now fired');
          }
          });


          More info: https://www.simoahava.com/gtm-tips/hitcallback-eventcallback/






          share|improve this answer












          When pushing to dataLayer with GTM you can use the eventCallback property.



          dataLayer.push({
          'key1' : 'value1',
          'key2' : 'value2',
          'event' : 'fireTags',
          'eventCallback' : function() {
          alert('ALL tags which fire on {{event}} equals fireTags have now fired');
          }
          });


          More info: https://www.simoahava.com/gtm-tips/hitcallback-eventcallback/







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 15:20









          Niels Van Looy

          464




          464






















              up vote
              0
              down vote













              In reference to Eike's answer. I created the following workflow to get this working;



              In my code I add to the dataLayer



              <script type="text/javascript">
              dataLayer.push({
              'event': 'gtm_redirect_url',
              'gtm_redirect_url': '<?php echo $url; ?>'
              });
              </script>


              In this case I'm using PHP to change the redirect url based on certain conditions.



              Then in GTM I set up a variable called gtm_redirect_url



              enter image description here



              And a trigger also called gtm_redirect_url;



              enter image description here



              And a tag to fire the JS in an HTML snippet



              enter image description here



              All pretty simple to set up and does ensure GTM has loaded before the redirect. This method also makes it easy to add the redirect data layer tag to any page you wish.






              share|improve this answer

























                up vote
                0
                down vote













                In reference to Eike's answer. I created the following workflow to get this working;



                In my code I add to the dataLayer



                <script type="text/javascript">
                dataLayer.push({
                'event': 'gtm_redirect_url',
                'gtm_redirect_url': '<?php echo $url; ?>'
                });
                </script>


                In this case I'm using PHP to change the redirect url based on certain conditions.



                Then in GTM I set up a variable called gtm_redirect_url



                enter image description here



                And a trigger also called gtm_redirect_url;



                enter image description here



                And a tag to fire the JS in an HTML snippet



                enter image description here



                All pretty simple to set up and does ensure GTM has loaded before the redirect. This method also makes it easy to add the redirect data layer tag to any page you wish.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  In reference to Eike's answer. I created the following workflow to get this working;



                  In my code I add to the dataLayer



                  <script type="text/javascript">
                  dataLayer.push({
                  'event': 'gtm_redirect_url',
                  'gtm_redirect_url': '<?php echo $url; ?>'
                  });
                  </script>


                  In this case I'm using PHP to change the redirect url based on certain conditions.



                  Then in GTM I set up a variable called gtm_redirect_url



                  enter image description here



                  And a trigger also called gtm_redirect_url;



                  enter image description here



                  And a tag to fire the JS in an HTML snippet



                  enter image description here



                  All pretty simple to set up and does ensure GTM has loaded before the redirect. This method also makes it easy to add the redirect data layer tag to any page you wish.






                  share|improve this answer












                  In reference to Eike's answer. I created the following workflow to get this working;



                  In my code I add to the dataLayer



                  <script type="text/javascript">
                  dataLayer.push({
                  'event': 'gtm_redirect_url',
                  'gtm_redirect_url': '<?php echo $url; ?>'
                  });
                  </script>


                  In this case I'm using PHP to change the redirect url based on certain conditions.



                  Then in GTM I set up a variable called gtm_redirect_url



                  enter image description here



                  And a trigger also called gtm_redirect_url;



                  enter image description here



                  And a tag to fire the JS in an HTML snippet



                  enter image description here



                  All pretty simple to set up and does ensure GTM has loaded before the redirect. This method also makes it easy to add the redirect data layer tag to any page you wish.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered May 29 at 12:06









                  Novocaine

                  3,49032948




                  3,49032948






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f32438499%2fmake-sure-google-tag-manager-and-google-analytics-is-done-before-redirect%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      What visual should I use to simply compare current year value vs last year in Power BI desktop

                      Alexandru Averescu

                      Trompette piccolo