GUA - Non Interaction for Dimensions - javascript

I am working on Google Universal Analytics and I see that our Dimensions are being captured by Google about 60% of the time. I see that the Dimensions are being set and I have verified in the Network that the Dimension is being sent to Google. Someone suggested that I look into setting {'nonInteract': 1}. I am confused where I should set the nonInteract flag to (ie - should I be setting this to GA() when I am setting the Dimension, or should it be set to the GA() when I am sending the event?
I have tried the following below, but I am confused with the result. In the Network tab, I do see that the Dimension is being sent to Google. When I inspect the Console with the GUA Debugger Tool, trying to set the dimension results in a Command ignored. Unknown target: undefined.
//Two variations I have tried when implementing the `nonInteraction` flag:
ga(u.name + '.send', 'event', category, action, label, {'nonInteraction': 1});
ga('set', 'dimension' + cvSlot, label, {'nonInteraction': 1});
Screenshot of console when cannot set Dimension15:
Source of where I got the nonInteract code:
https://developers.google.com/analytics/devguides/collection/analyticsjs/events
My question is how do I attach the nonInteraction flag to my Dimension?

Setting a non-interaction flag on a set call for a dimension would be pointless since the set call is not an interaction in the first place. An Interaction is a hit that sends information to the Google servers (send pageview, event, timing, ecommerce item and transactions). You need to have an interaction hit to send a custom dimension. The only hit type you can set to non-interactive is event. However a set call after an event is pointless since the hit has already been sent and the dimension cannot be added to the hit (and it might even be harmful, since a set call attaches the dimension to all subsequent hits, interactive or not).
So the best way would be to use a non-interaction event and attach the dimension to it via it's configuration object.
ga('send', 'event', 'category', 'action', {
'nonInteraction': 1}
'dimension': myvalue
});
Non-Interaction means that the event will not affect the bounce rate, so it's not like the setting will have a huge effect (it will only affect people who trigger an event on their first pageview and then leave without looking at more pages).

Related

Will dataLayer.push() definitely send data to google when triggered on an anchor?

This may seem like a simple question, but it doesn't seem to be answered anywhere that i can find.
I am writing an onClick event handler that simply calls dataLayer.push() when an anchor is clicked.
Is dataLayer.push() a synchronous operation?
Will the GET request to google definitely be sent, even though the browser has unloaded the page it was requested from due to the link being followed?
Some browsers show the connection get cancelled, some show it success.
My question is if the computer is slow, is it possible for the page to get unloaded before the request is sent?
This is why i assume that google started using the eventCallback property to redirect the user after the link has been followed.
e.g.
https://developers.google.com/tag-manager/enhanced-ecommerce#product-clicks
This source code does not include the click handler, but implies that the onClick event should stop propogation and let the eventCallback function set document.location.
However, as soon as you cancel the event, all its information has gone.
This (in my opinion) is just the wrong way to do it.
e.g.
(CTRL or COMMAND) + Click opens a new tab on browsers. This will not work unless the onClick event handler allows the prorogation to continue.
Relying on eventCallback also means that if the google scrips didn't load for one of the many reasons it could (but is still unlikely), your links don't work. And your site is broken.
So this leaves the correct way to do it for the onClick event handler to allow the event to propagate and return true.
Which also means that dataLayer.push() would need return after the GET request was sent for any of this to work properly.
Code example:
NOTE: You will get mixed results in mixed environments.
Link
$(document).on('click', 'a', function(event) {
// Is dataLayer.push() guaranteed to fire a GET ?
// data set externally
dataLayer.push(data);
return true;
});
Is there anyone out there that can guarantee that the GET request will get fired to the google server?
Have the google developers forgotten something here?
EDIT: Updated title to be more relevant to the question.
datalayer.push does not send anything to Google. It pushes objects with key/value pairs to the datalayer array. This might contain an event which in turn fires a tag. Whether the tag is sent depends on the setup of the tag, not on the dataLayer.push.
As a consequence, when you write your own click handlers your are yourself responsible to make sure your tags are actually fired.
If you use the built-in click handler you can configure a delay to make sure your tag has time to fire before the link redirects:
Since link clicks usually cause the browser to load a new page and
interrupt any pending HTTP request, you have the option to add a small
delay to allow tags fired by Tag Manager to execute properly before
redirecting to the next page. Checking the “Wait For Tags” option will
delay opening of links until all tags have fired or the specified
timeout has elapsed, whichever comes first.
You should be able to mix both methods (push data on the click, but still use the "native" link click handler for the event).
You can also try to specify "beacon" as the transport method in your Google Analytics tags, on browsers that support this (which I think is only Chrome at the moment) GA will then use the navigator.sendBeacon interface, which sends the data even in case the page unloads.
You might think that Google's solution is not very elegant (but the simple delay has the advantage that it works for all tags, not just for GA), but they have not "forgotten" the problem.
Also solutions that combine GA hit callbacks with timeouts that redirects if the callback fails as proposed i.e. by Simo Ahava somewhere should be be doable with GTM, even if they are probably more cumbersome to implement in GA.

How to set a custom Dimension in GTM parallel to the UA tracking preset?

i looking forward for a solution to set a custom dimension outside of the UA preset in the Google Tag Manager.
The Problem is that the Data for the dimension is not everywhere available. So i can not set dimension within the preset, i guess.
The usecase:
I have a zip code query in a layer to find out if the user is suppliable or not.
This information i will send to GA with a custom dimension.
Actually i listening for the div id of the two layer versions which are displayed after the query. Then i set the dimension like below but the UA Tag is then already fired on the page view:
ga('set', 'dimension6', 'Yes');
Thanks for your reply! Hope my english is good enough. ;)
I assume by preset you mean the fields for custom dimensions in the GA tag template. You can use that without problems; if a GTM variable is not set it defaults to "undefined", and undefined values are ignored when you send them in custom dimensions.

Google Analytics Event Unique Per Session

I have a site where I track click outs, one user may click out several times so if I have 100 users I may have 300 click out events. This is ok for a rough guide but ideally I would like to know how many users have clicked out so 100 users 85 users clicked out.
I could write some js on my site to handle this store in a cookie but I was wondering if there is anything in ga.js that will allow me to fire an event once per session.
You can use Custom Dimensions to scope things to the session (or even the user). Create a dimension in the Google Analytics admin tab under Property within Custom Definitions.
Set the scope to session, then copy the ga('set', 'dimension1', 'Description Here'); and paste it before sending the event (obviously, change "Description Here" to your own explanation. Also, make sure 'dimension1' matches the index it gives you.
Custom dimensions (and metrics) are tied to hit types, so simply setting them does nothing until a hit type is sent- so either a pageview or an event.
Once you've created the dimension, you can use it for anything you would a standard dimension in reporting- including segments and custom reports.
Hope that helps!

Custom Dimension with "Hit" Scope passing empty value for other hits

I have a HTML single page application (it is a single HTML file, where i show page navigation as hiding and showing multiple div elements, so page navigations are not actual page loads, just switching divs)
I have created some 5 custom dimensions of "Session" scope, so they will be always present and tracked with some value.
However, I need a 6th dimension which will be populated only upon doing a particular operation such as clicking a button. So i have created 6th dimension and set its scope as "Hit". In the HTML application, as soon as the user clicks the button, i set the value for that dimension and do a send for that request (it is actually an 'ec' tracking), i can see that the 6th dimension value is also set.
The problem is, After this tracking, if i track any other event tracking or page view tracking, the same value is being passed for the cd6 parameter.
Could you please let me know if i am doing anything wrong here?
If you set a custom dimension via set it will apply to all hits that follow:
ga('set', 'dimension1', 'mydata');
will attach the dimension to all following tracking calls (especially bad on a single page app).
You can pass a JSON object as second parameter to the ga("send") call instead and set the dimension there:
ga('send', {
'dimension1': 'mydata',
});
That way the dimension is only sent with that pageview, later interactions will not be affected.

how can a userscript get notified about ajax-driven changes on the page?

Given items on a webpage that get changed by AJAX calls over time, how can a userscript get notified about those changes, whenever they occur?
Imagine the Facebook newsfeed. It has 12 items in it when you load the page. Those items are wrapped in <li> tags contained within a <ul>. As you scroll the page down, new <li> chunks of data load into that <ul>.
I'm wondering how a userscript could be notified of such a change.
One idea is to constantly query that <ul>, counting its items, and watching to see if that number gets bigger. Possible, but to catch the change right when it happens it might have to run so often that it's too expensive.
Another idea would be to figure out what scroll position triggers the loading, and to watch for such a change. Less expensive, but very specific.
I'm wondering if there's a third option. Something that would notify me of the change, whenever it happens. I'm not just interested in the feed, but in this concept more generally. Given items on a page that get changed by AJAX calls, how can a userscript get notified about those changes?
Hijack the send method
var oldSend = XMLHttpRequest.prototype.send;
XMLHttpRequest.prototype.send = function(){
// do what you need; then send the request
oldSend.apply(this, arguments);
}
I think what you are looking for is the DOMSubtreeModified event.
This works in firefox chrome and IE >= 9, if your scripting on facebook im guessing its for a greasemonkey/chrome extension? if that is the case this should be okay.
This event is fired on a node when ever a child node is added removed or changed
You can use it with
.addEventListener ("DOMSubtreeModified", handler, useCapture);
but I don't think it works with attachEvent.
Here's some more info on it.
http://help.dottoro.com/ljrmcldi.php
Do you have access to the Ajax calls that are updating the pages contents? Generally the better approach is to attach a call back to the actual Ajax call.
If the request are being made with Jquery use $.ajaxComplete() or $.ajaxSuccess() to trigger your code. These will fire any time a request completes so when this happens you can check if the content has changed without it being to expensive.
$.ajaxSuccess(function() { //check for update and do something });

Categories

Resources