Google Analytics Events No Longer Working In Universal Analytics & GTM - google-analytics

I had a script using the older Google Analytics tag that was directly embeded on my page. This script fired an event when it detected that ad block was being used:
<!--AB Check-->
<script type = "text/javascript"
src = "http://example.com/JS/Ads3.js"> </script>
<script type = "text/javascript" > jQuery(document).ready(checkAds());
function checkAds() {
if (document.getElementById('adsense') != undefined) {
document.write("<script>_gaq.push(['_trackEvent', 'Adblock', 'Unblocked', 'false',,true]);</sc" + "ript>");
} else {
document.write("<script>_gaq.push(['_trackEvent', 'Adblock', 'Blocked', 'true',,true]);</sc" + "ript>");
}
} </script>
<!--End AB Check-->
Recently I moved my Google Analytics tag to Universal Analytics and Google Tag Manager. At that time my script broke and no longer records these events.
How can I update my adblock script to be compatible with Universal Analytics and GTM?

If you weren't using GTM, then you need to change the event tracking code to use the ga function:
ga('send', 'event', 'Adblock', 'Unblocked', 'false', {nonInteraction: true});
ga('send', 'event', 'Adblock', 'Blocked', 'true', {nonInteraction: true});
More information about this event tracking code can be found here: https://developers.google.com/analytics/devguides/collection/analyticsjs/events
But since you are using GTM, what you could do is to create your event tag that is fired via custom JS trigger that returns a boolean, depending on the conditions for checking the adSense ID.

I ended up using this article http://dcarlbom.com/google-tag-manager/measure-how-many-of-your-visitors-that-are-using-adblock to set the event tracking up for GTM.
Thanks #nyuen for explaining how to do this using the ga function, but unfortunately this won't work for folks using GTM.

Related

beforeunload event on Single Page Application (SPA) Website using Google Tag Manager (GTM)

I would like to track beforeunload event on SPA website, as I want to send data via GTM to datalayer. I want to track video watch time (HTML5 player) and send the data right before a user leaves the current page either to another one on the same website or completely different.
Is there a way to have this done via GTM custom HTML?
Assuming that you have a way to capture the video time in a variable, you can send that information to Google Analytics using an event listener for the beforeunload event. I’m doing that on one of my webpages to get an idea of how long users have the page open.
Here’s the code I’m using:
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID"></script>
<script>
// Disable tracking if the opt-out cookie exists
if (document.cookie.indexOf("ga-disable-GA_MEASUREMENT_ID=true") > -1) {
window["ga-disable-GA_MEASUREMENT_ID"] = true;
}
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag("js", new Date());
gtag("config", "GA_MEASUREMENT_ID", { "anonymize_ip": true, "cookie_flags": "SameSite=None;Secure" });
</script>
<script>
window.addEventListener("beforeunload", function (e) {
gtag('event', 'Time_Spent', {
'event_category': 'Time_on_page',
'event_label': e.timeStamp.toString,
'value': 1,
'transport_type': 'beacon'
});
delete e.returnValue;
});
</script>
The first section is in the head and the second section is at the end of the body.
I put the event.timeStamp value in the event_label; you could put your video time, formatted however you like into that field as well.
I’m not sure I need the optional value field, but I have it.
The transport_type beacon tells Google to transmit the data asynchronously when the User Agent has an opportunity to do so, without delaying unload or the next navigation.
To see how long people have been spending with my page open, I go to:
Google Analytics -> Behavior -> Events -> Overview
I click on the event category (Time_on_page in my case), and then click on Event Label to get a listing of all of the values.

Issue with Dynamic Remarketing Tag. The Product ID parameter is missing in 99% of the activity

When I go to Tag Google Ads details, I have this issue: "The “Product ID” parameter is missing in 99% of the activity of your Google Ads Tag".
Note, I implemented Dynamic Remarketing Tag via Google Tag Manager. In setting up Tag "Remarketing Google Ads" I inserted:
ID Conversion: XXX-XXX-XXX;
Custom Parameters: Use Data Layer {{google_tag_params}};
Trigger: All pages.
Following you can see source code html for a product page that contains ecomm_prodid, ecomm_pagetype, ecomm_totalvalue:
<!-- BEGIN: Google TagManager Remaketing -->
<script>
var dataLayer = window.dataLayer || [];
dataLayer.push({
'event':'remarketingTriggered',
'google_tag_params': {
'ecomm_prodid': '721',
'ecomm_pname': 'MAGLIONE GIROCOLLO IN LANA',
'ecomm_pcat': 'home',
'ecomm_pagetype': 'product',
'ecomm_totalvalue': '20.00'
}
});
</script>
<!-- END: Google TagManager Remaketing -->
So, I created in GTM a variable called google_tag_params with:
Data Layer Variable Name: google_tag_params;
Data Layer Version: Version 2.
As seen before, I used this variable in Remarketing Google Ads Tag, trying to capture at least: ecomm_prodid, ecomm_pagetype, ecomm_totalvalue needed to Display Remarketing.
When I use GTM in preview mode I have this:
Send dynamic remarketing event data: false
Variabile di livello dati:
{ecomm_prodid: '721',
ecomm_pname: 'MAGLIONE GIROCOLLO IN LANA',
ecomm_pcat: 'home',
ecomm_pagetype: 'product',
ecomm_totalvalue: '20.00'}
ID conversione: 'XXX-XXX-XXX'
Parametri personalizzati: 'DATA_LAYER'
Problem is issue "The “Product ID” parameter is missing in 99% of the activity of your Google Ads Tag" is still there.
I tried also check via Tag Assistant with Product Feed ID XXX-XXX-XXX, but I got this alert: Some products are not valid.
When I check on Metadata request in Tag Assistant I got this "event:gtag.config" and not parameters.
I searched for days on web, but did not find any solution.
Could you please assist me?
Thanks,
Francesco
Try the following:
Create a dataLayer variable for each of your dynamic remarketing variable.
google_tag_params.ecomm_prodid
google_tag_params.ecomm_pname
google_tag_params.ecomm_pcat
google_tag_params.ecomm_pagetype
google_tag_params.ecomm_totalvalue
Use the following custom event as you have in the dataLayer event field.
Use the manually specify on your AdWords tag:
Verify the implementation by checking in your browser network tab, filtering by the keyword random. You should see the dynamic remarketing request along with the key/value you've provided.

Value not passing to Facebook using GTM

I have a tag that sends purchase event to Facebook that looks like this:
<script>
fbq('track', 'Purchase', {
value: {{reservationValue}},
currency: 'ILS',
});
</script>
The reservationValue is a variable that the GTM gets from the page before the tag is fired.
For some reason, when i'm using the preview mode, I see that the HTML code that is sent to Facebook is:
<script type="text/gtmscript">fbq("track","Purchase",
{value:google_tag_manager["GTM-XXXXXXX"].macro(2459),currency:"ILS"});</script>
screenshot here
The same variable is used to send value of purchase to AdWords and when I preview the AdWords tag, the value is passing as it should.
I did Tag Sequencing and fired the pageview Facebook pixel before the purchase tag.
Any suggestions?
Thanks
I think this is normal behaviour because the syntax of GTM variables with the double curly brackets is the GTMs own syntax. what you saw there happens to every GTM variable. The GTM variables get "translated" to regular javascript again.
If you open the console within a browser and type in "google_tag_manager["GTM-yourContaienrId"].macro(2459)" you should be able to se the value assuming the Tag has been fired of course. You could also console.log the variable to see if it is being filled correctly.
The reason why you didn't see that behaviour on the AdWords tag is probably because it is a "native" tag type in the GTM and the other one is just plain html.
Change into:
<script>
fbq('track', 'Purchase', {
value: '{{reservationValue}}',
currency: 'ILS',
});
</script>

Google tag manager don't do analytics

I have Google tag manager tag inserted on all pages on the website.
head:
<!-- Google Tag Manager -->
<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 -->
At the end of body:
<noscript>
"<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-MDKNR5" height="0" width="0" style="display:none;visibility:hidden"></iframe>"
</noscript>
On homepage and four other pages on website, this code works OK
On those pages, gtm head element (I suppose), creates two new lines above him.
<script type="text/javascript" async="" src="https://www.google-analytics.com/analytics.js"></script>
<script async="" src="https://www.googletagmanager.com/gtm.js?id=GTM-XXXXXX"></script>
But on two remaining pages, only one line is created
<script async="" src="https://www.googletagmanager.com/gtm.js?id=GTM-XXXXXX"></script>
I went deep in that gtm.js file and found:
if (!Ck) {
var Z = a[oc] ? "u/analytics_debug.js" : "analytics.js";
a[kd] && !a[oc] && (Z = "internal/" + Z);
Ck = !0;
r(L("https:", "http:", "//www.google-analytics.com/" + Z, u), function() {
x[l()].loaded || d()
}, d)
}
That returns false, and don't do that part of code. So, I don't have analytics on that page.
Chrome extension "Google Tag Assistant" gives me this notification:
This page did not send a hit, but other recorded pages on the same
domain did send hits. Most likely, this page was not properly tagged
with the Google Analytics tracking code
In the end, to mention, I don't have access to that Google account, I only got tracking codes for GTM and analytics.
If the tracking code for Google Analytics should fire or not is set within the GTM interface. If, for some reason, there is a blocking trigger set to not fire Analytics on those pages, that is the most likely reason. It is basically impossible to answer your question without access to the configuration itself or more information from the GTM account and the way it is set up.

Google Tag Manager - How to avoid data loss

I am using Google Tag Manager to register events with Google Analytic. At one plance I am changing url on the changeof a dropdown. I want to track the same event on Google Analytics. I am worried what would happen if page is changed before event is registered with GA. Could you please let me know if there is a feature in GTM that can ensure that page is not changed before event is registered with GA.
Here is the code that will execute on the change of the dropdown
var targetCityChangedEventName = "TargetCityChanged";
$("#location", topHeader).bind({
"change": function(ev, obj) {
dataLayer.push({event : targetCityChangedEventName });
var url = "http://" + window.location.host + "/" + $(this).val();
window.location = url;
}
});
If you are using ga.js (asynchronous Analytics) you can set an hit callback (a macro that returns a function) in the tag template under "advanced configuration and do the redirect there (possibly you'd need a separate analytics tag just the change event).
If you use Universal Analytics there was a discussion at the Tag Manager Google Group a while ago where Googles Brian Kuhn suggested the following way ( I have not tested this):
In the meantime, have you tried this?
dataLayer.push({callback:
function() {
alert(123);
});
Then, create a dataLayer macro that reads the "callback" key. Then,
use that macro as the value of a "fields to set" pair on your UA tag,
under the field name "hitCallback".
Instead of the alert you'd do the redirect.
In case that's not clear, a hit callback is a function that can be passed to the tracking calls and is executed after the tracking call has executed.
I was able to resolve the same issue by simply inserting a delay. The dataLayer.push doesn't need to return anything, so a 100 millisecond delay is sufficient in 99% of the cases for the dataLayer.push to be executed.
dataLayer.push({ ... });
setTimeout( function(){ window.location = ...; }, 100 );
Note that the GTM preview/debug mode gives false positives - you must make sure that your tags are actually being fired, in my case my event was for a virtual pageview and I could see the results in RealTime Analytics. Without the delay, I could see that the tag was not being fired.

Resources