Make sure Google Tag Manager and Google Analytics is done before redirect
up vote
4
down vote
favorite
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
add a comment |
up vote
4
down vote
favorite
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
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
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
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
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
javascript google-analytics google-tag-manager
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
add a comment |
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
add a comment |
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.
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
add a comment |
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:
This flag ensures that the hit is sent before the page navigates away.
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
add a comment |
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/
add a comment |
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
And a trigger also called gtm_redirect_url
;
And a tag to fire the JS in an HTML snippet
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.
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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:
This flag ensures that the hit is sent before the page navigates away.
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
add a comment |
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:
This flag ensures that the hit is sent before the page navigates away.
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
add a comment |
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:
This flag ensures that the hit is sent before the page navigates away.
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:
This flag ensures that the hit is sent before the page navigates away.
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
add a comment |
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
add a comment |
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/
add a comment |
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/
add a comment |
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/
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/
answered Nov 22 at 15:20
Niels Van Looy
464
464
add a comment |
add a comment |
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
And a trigger also called gtm_redirect_url
;
And a tag to fire the JS in an HTML snippet
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.
add a comment |
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
And a trigger also called gtm_redirect_url
;
And a tag to fire the JS in an HTML snippet
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.
add a comment |
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
And a trigger also called gtm_redirect_url
;
And a tag to fire the JS in an HTML snippet
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.
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
And a trigger also called gtm_redirect_url
;
And a tag to fire the JS in an HTML snippet
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.
answered May 29 at 12:06
Novocaine
3,49032948
3,49032948
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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