Implementing Enhanced Link Attribution - google-analytics

On the GA admin page, you have to check the section Use enhanced link attribution to activate Google Analytic's Enhanced Link Attribution.
But how does the code look like? Even if the feature is new, many sources seem to be dated. Is the code Google publishes on their doumentation page correct and complete, or should it be something else?
var _gaq = _gaq || [];
var pluginUrl =
'//www.google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]);
_gaq.push(['_setAccount', 'UA-XXXXXX-Y']);
_gaq.push(['_trackPageview']);

Yes that is correct, my full code looks like this
var _gaq = _gaq || [];
var pluginUrl ='//www.google-analytics.com/plugins/ga/inpage_linkid.js';
_gaq.push(['_require', 'inpage_linkid', pluginUrl]);
_gaq.push(['_setAccount', 'UA-xxxxxxxx-1']);
_gaq.push(['_setDomainName', 'domainname.com']);
_gaq.push(['_setAllowAnchor',true]);
_gaq.push(['_setCustomVar',5,'CusVarName','CusVarNameValue',3]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
But if your setup is basic and according to the example you have given above it should work just fine. As long as you add the pluginUrl variable above the setAccount it should work.

Related

Google Analytics custom variables not showing up

I have been running this code for some time now on all pages. This custom variable has never shown up.
Sorry to ask this again, I don't see another post that solves my case. This has to be the simplest case in the world, this is the same code with only the UA changed:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12345678-1']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setCustomVar', 1, 'User', 'jc', 3]);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
I did not modify the (function()...) .. Do you see the problem? I'm using slot 1 with the page level (3) scope. The same code appears on each page for a given user.
_setCustomVar does not trigger a request to GA. _trackPageview does. You have to move the _setCustomVar above the _trackPageview.

Google Analytics cross browser tracking only shows own domain as source in Goal Flow

I have set up cross browser tracking in google analytics in order to track goals. However at Goal Flow I only see my domain (mysite.com) listed at Visits by Source. I have set up tracking in the following way:
All non secure pages of my website contain the following code:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-Y']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setDomainName', '.mysite.com']);
_gaq.push(['_setAllowHash', false])
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
The secure pages of my website, where the goals are realized, contain the following code:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-Y']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setDomainName', '.my-securesite.com']);
_gaq.push(['_setAllowHash', false]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
The form that pushes data from the non-secure page to the secure page contains the following data:
<form id="21343" method="post" action="https://my-securesite.com" onsubmit="return _gaq.push(["_linkByPost", this]);">
Looking at the source of the page I do see all the utmx data being posted:
action="https://my-securesite.com/confirm.php?__utma=XXXXXXXXXXX" onsubmit="return _gaq.push(["_linkByPost", this]);">
Once I access mysite.com via for example a search on Google I do see the following utmx definition when posting data to my-securesite.com:
utmcsr=google|utmccn=(organic)|utmcmd=organic|
So apparently the code posts the right source....
What am I doing wrong here?
I discovered the reason why GA didn't display the right source for my goals:
I used this tracking code:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-Y']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_setDomainName', '.mysite.com']);
_gaq.push(['_setAllowHash', false])
_gaq.push(['_trackPageview']);
I was informed that the right tracking code now looks like:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-Y']);
_gaq.push(['_setDomainName', '.mysite.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
As _gaq.push(['_setAllowHash', false]) is no longer used.
Besides that I used GA's option to track additional pageviews at the checkout page. I used to work with this code:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-Y']);
_gaq.push(['_trackPageview','/funnel/G1/Page-name']);
</script>
However the right code is:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-Y']);
_gaq.push(['_setDomainName', '.my-securesite.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview','/funnel/G1/Page-name']);
</script>
Since I implemented these changes GA tracks the source of my goals correctly.

Uncaught ReferenceError: _gaq is not defined (Google Analytics)

The following message appears when viewing a site page in the chrome debug log.
Uncaught ReferenceError: _gaq is not defined
The page itself is supposed to track an object using the onload event handler and fire a _trackEvent for Google Analytics.
My best guess is that perhaps the ga.js file doesn't load in time and therefore the onload _trackEvent triggered is not caught. the async snippet is being used before the </body> close and the object is positioned in the middle <body>.
(some other posts have referenced jQuery position too but, this might be a red herring)
any assistance greatly appreciated.
From https://developers.google.com/analytics/devguides/collection/gajs/ this code replaces your existing "traditional snippet" with the "latest, asynchronous version, you should remove the existing tracking snippet first."
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
To see your events come in (to know if this is working) look for "Events" under the "Real-Time" menu option on the left side of the "Reporting" page.
Regarding the position of the async snippet, the GA help page says -
Paste this snippet into your website template page so that it appears
before the closing </head> tag.
My first thought was that JS should be loaded at the bottom of the page to improve page speed. However, the GA async tracking snippet should be loaded in the head, as it will not load the ga.js immediately, and it won't block page execution.(It does this by dynamically adding the script tag to the DOM, which puts it at the back of the queue.)
If, for some reason, you can't move the async snippet to the head, you can define _gaq yourself, like this-
<button onclick="var _gaq = _gaq || []; _gaq.push(['_trackEvent', 'button3', 'clicked'])"/><button>
Had the same problem. You have to define the _gaq array. Just add this after your Google Analytics script in the header:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXX-X']);
_gaq.push(['_trackPageview']);
You can use the last version of analytics.js instead of ga.js
ga.js is a legacy library. If you are starting a new implementation we
recommend you use the latest version of this library, analytics.js.
For exisiting implementations, learn how to migrate from ga.js to
analytics.js.
Here is an example:
ga('send', {
hitType: 'event',
eventCategory: 'Video',
eventAction: 'play',
eventLabel: 'cats.mp4'
});
Change the Tracking Code to:
<script type="text/javascript">
var gaq;
var _gaq = gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setDomainName', 'yourdomain.com']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script');
ga.type = 'text/javascript';
ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www')
+ '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0];
s.parentNode.insertBefore(ga, s);
})();
</script>

Google Analytics Custom Variables not working

I am trying to log the username of users who are using my system. Unfortunately, I couldn't track by using custom variables. I could track the page visit, but custom variable tracking is not working. Could you please help me? Thanks.
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxxx-1']);
_gaq.push(['_setDomainName', 'none']);
_gaq.push(['_setCustomVar',1,'UserName','JohnPaul',2]);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
The code is correct and it does track the users now. I just used the similar codes as above and it works now. But I needed to wait 2 days to track the custom variables and it appears in my reports now.
For more than one user, you'll have to use PHP to output user names.
_gaq.push(['_setCustomVar', 1, 'UserName', '<?php get_usernames(); ?>',2]);
***Substitute "get_usernames" with the function for your site that retrieves the usernames.

Analytics custom variables

I'm trying to make a custom report.
I've just put some test code on my main page:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12323748-3']);
_gaq.push(['_trackPageview']);
_gaq.push(['_setCustomVar',
2, // This custom var is set to slot #1
'Test_var', // The name acts as a kind of category for the user activity
'Yes', // This value of the custom variable
2 // Sets the scope to session-level
]);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
My custom report setup looks like this:
http://updo.nl/file/ed351e11.png
However, it just comes up as empty (I let it gather data for 2 days before checking the report)
Any help would be appreciated
I've asked much the same question. I've been told that you have to put the setCustomVar line above the trackPageView line and below the var _gaq line. Like this:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-12323748-3']);
_gaq.push(['_setCustomVar', 2, 'Test_var', 'Yes', 2]);
_gaq.push(['_trackPageview']);
_trackPageview sends all of the information to Google Analytics, so you can't add information after its already sent.

Resources