How can I make sure that gtm send the pageview fast? - google-analytics

I am making the change from the basic analytics code to google tag manager since I need it to track impressions for certain elements. While testing when the pageview fires, I get this warning :
"This page took a long time to send a pageview hit to Google Analytics. Users could be clicking away from your site before it records a pageview."
Google tag manager requires adding the code at the beginning of the body (I tried adding it in the header just in case it helps with that warning, but that didn't work).

Probably GA is not the best choice for such task. GA is well-tailored for pageviews, but not for custom actions.
I solve similar issue with http://www.devmetrics.io analytics. To track impressions for certain elements on my page: 1) add for each of this elements trackvisible css class 2) using setInterval check visibility every few seconds.
Example code using jQuery and devmetrics analytics:
$('.trackvisible').each(function( index ) {
if ($(this).visible()) {
var tag = 'element.' + $(this).attr('id');
devmetrics.userEvent('impression', [tag]);
}
});

Related

Tracking user link clicks with google analytics

I have created an affiliate marketing website. Here users will register on my website and shop, however the shopping link will be of amazon.
I need to know how to set up Google Analytics so that it can track which registered user has clicked an affiliate link on my website.
For example I have this amazon link
https://www.amazon.in/BassHeads-225-Super-Extra-Headphones/dp/B01M9C51T9/ref=sr_1_1?_encoding=UTF8&pf_rd_i=desktop&pf_rd_m=A1VBAL9TL5WCBF&pf_rd_p=14ffe21a-e5a2-45c9-a9c0-91f47b082bbc&pf_rd_r=94G5Y8CM93G8M5Y2SNS3&pf_rd_t=36701&qid=1554008093&s=gateway&smid=A14CZOWI0VEHLG&sr=8-1
I know I'm supposed to put an onClick event on there somewhere but I don't have any idea how it links to Google Analytics? I am using Google Tag Manager
Is this the correct Onclick code:
onClick="_gaq.push(['_trackEvent', 'Link', 'Click', 'Banner Advert1']);"
If not what do I need to add to track the registered user?
I have many links to external sites on my website; an example link looks like this:
<a id="Buy" data-itemDescription="(a description)" data-itemValue="2.80" href=http://externalsite?id=6789>
The key things here are the extra attributes before the href: id (used to identify the specific event that occurs when the link is clicked, i.e. in this case a Buy event) and the data-itemDescription and data-itemValue metadata (used in constructing the event label etc.)
The relevant GTM artefacts are as follows:
Trigger:
Buy: Click - Just Links when Click Id contains Buy
User-defined variables (custom Javascript):
ItemDescription:
function() {
return {{Click Element}}.getAttribute("data-itemDescription");
}
ItemValue:
function() {
return {{Click Element}}.getAttribute("data-itemValue");
}
ItemValueFloat (100 multiplier used because GA didn't like my decimal places - I then divide by 100 at reporting time to get the right answer):
function() {
return parseFloat({{ItemValue}})*100;
}
Tag BuyClicked is an Event triggered by the Buy trigger as above, with:
Category = Purchase Tracking
Action = Purchase {{ItemDescription}}
Label = {{Page Path}} : {{Click URL}}
Value = {{ItemValueFloat}}
I also have Non-Interaction Hit set True.
The BuyClicked event is then collected by GA, and reportable on from e.g. Google Data Studio, without my having to do anything further.
If you can't add an id or metadata, you could undoubtedly do the same sort of thing more painfully by handling all clicks through a single trigger and then parsing the Click URL via custom Javascript to get granular Categories, Actions, Labels.

Google Analytics returning many "undefined" events

I've got a fairly simple Tag Manager/Analytics set up (firing events on clicks/visibility) and it's working as expected for the tags I've set up myself. However when I check the Analytics page there are thousands of "undefined" tags.
I've had a look at all of the tags I created and none of them should be returning "undefined" as the category/action (the label is "(not set)")
All I need is for the undefined events to be ignored (or even better to stop firing) in Google Analytics, as it's getting in the way of the client analysing the data.
Any help is appreciated
EDIT:
As suggested, here's the only User-Defined variable on the Tag Manager:
https://imgur.com/a/flcCpGo
Here's an example of one of the tags:
https://imgur.com/a/eWM22FB
and here's a tag being fired in the preview mode:
https://imgur.com/a/0ipwqhr
May be some other (hardcoded) event returns 'undefined'?
Try to check in Chrome using «Dev Tools», tab «Network», filter google analytics calls.
And I notice you send 'gtm.click' (string) in field 'Value' that must contain integer.
Turns out this was simply just due to a tag not having a category/action/label that had been paused a while ago, the Analytics timeframe was covering from before when this tag was paused, leading it to show the undefined tags.

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.

Can I avoid double tracing if parent and iframe runs the same Google Analytics

How can i use event tracking feature in a iframe if the parent document uses the same Google Analytics ID but i don't want to double the pageview number?
Can I disable the default page tracking or switch to "manual tracking"?
The story in brief: The parent document displays the comments in a iframe and i want to track some clicks inside the comment frame. I don't want to use different GA account to parent and child and also I don't want to merge the iframe into the parent document. I hope you can help me, I'll give you a FREE virtual BEER if you want some.
Iframe documents can detect that they are being iframed.
You can revert the logic and fire the GA tracking code only when outside of a frame:
if (window.self === window.top) { /* I'm NOT in a frame! */
// GA tracking code
_gaq.push(['_trackPageview']) ;
}
Does this answer your question ?

How to incorporate Google Analytics Event Tracking with Anythingslider

I am looking for some clues on where to add Google Analytics Event Tracking to a page that has AnythingSlider installed. I have a slider that does not autoscroll and you have to click the navigation buttons for the slides to move. I wish to track these clicks. I would also like to track if a visitor clicks on a link within a slide.
I am wishing to use Google's InPage Analytics to track visitor click behavior and workout what items (images & phrases) catch the attention of the visitor in order to make better lead funnels.
I did try the Event tracking guide from Google before posting here, but I was unsure of where to pickup the navigation clicks from the slider.
add this to any a href in your html
onClick="_gaq.push(['_trackEvent', 'Slider','Panelno:x', 'blah']);"
Untested (I'm about to do this myself) but the API suggests to me that I should use the onSlideComplete callback, e.g. (assuming you've set up the names of your panels in an array called pages).
onSlideComplete : function(foo) {
_gaq.push(['_trackEvent', 'Slider','Panelno:'+slider.currentPage, pages[index - 1]]);
}
EDIT: Be careful with onSlideComplete, it seems to fire off too often. Set a var to tell it not to refire, and reset the var with onSlideBegin.
onSlideBegin : function(e, slider) {
refire = 1;
},
onSlideComplete : function(slider) {
if (refire) {
_gaq.push(['_trackEvent', 'Slider','Panelno:'+slider.currentPage, pages[slider.currentPage - 1]]);
refire=0;
}
}

Resources