My Custom Variables are not showing up in Google Analytics - google-analytics

I have been trying to setup a custom variable for the past few days and it hasn't been working.
My Google tracking code is part of a master page (asp.net concept) so I can't set the custom variable inside the second script block labeled "Async Google Code" because it is shared by many other sections.
Below is my code and the order it appears in my page. Is there any way I can set it outside the "Async Google Code" script?
<head>
<!-- Setting Custom Var -->
<script type="text/javascript">
$(function () {
_gaq.push(['_setCustomVar', 1, 'Account', 'UserType', 2]);
}
</script>
<!-- Async Google Code -->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXXXX-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>
</head>

wrap your custom variable stuff in a function to be called and insert a call to that function between the var _gaq... and _trackPageview lines in the bottom piece.

Your "setting custom var" code is missing an end bracket.

You need to set the custom variable before track pageview. You have the custom var in the jQuery document ready shortcut, making it so that it happens after the trackpageview.

Related

GA - Experiments does not work

we try to set up experiments in our site. We would like to testing 3 type of banner on our home page.
1. If anybody click on the banner, makes event, which si set up as a goal in GA. This goal measure clicking correctly.
2. We implement code for measuring experiments and made javascript function for changing banners, they changing well.
3. For set up whole experiments in GA and in code we follow this documentation: https://developers.google.com/analytics/solutions/experiments-client-side
Finnaly our whole GA script on site looks this:
<script src="//www.google-analytics.com/cx/api.js?experiment=sTe5dJkJTfmSO8YXsc7Kuw">
<script>
var variation = cxApi.chooseVariation();
cxApi.setChosenVariation(variation);
</script>
<script type="text/javascript">
var _gaq = _gaq || [];
(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>
<script type="text/javascript" src="http://oursite.cz/js/ga.js"></script>
<script type="text/javascript">
_gaq.push(['_trackPageview']);
</script>
Problem is, that experiment does not work. It does not measure anything. When we try to look on console, what data are sending, we realize, that ` correctly send hash Te5dJkJTfmSO8YXsc7Kuw, but that is all, There is no data of variation type, which was choosed by
<script>
var variation = cxApi.chooseVariation();
cxApi.setChosenVariation(variation);
</script>
It should sent 0 or 1 or 2, but it send nothing. What we make wrong? Thank you for answer,
You're missing the closing tag when loading the content experiment.
You have:
<script src="//www.google-analytics.com/cx/api.js?experiment=sTe5dJkJTfmSO8YXsc7Kuw">
Should be:
<script src="//www.google-analytics.com/cx/api.js?experiment=sTe5dJkJTfmSO8YXsc7Kuw"></script>

How to pass Google Analytics cookies from page contained in iframe, to page on different domain

Here is my problem:
We have page contained in iframe (url of iframe page - www.iframepage.com). That page has link UPGRADE on it. When user clicks on that link, he gets directed to billing page, which is located on different domain (www.billingsite.com/cc.html).
That page should be open on top (not in iframe).
If I use _link, GA cookie values will get passed to target page and cross domain tracking will work, BUT target page will open in iframe.
UPGRADE
https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApiDomainDirectory#_gat.GA_Tracker_._link
I would need a solution that will provide both: target page should open on top (not in iframe) and GA _utm parameters should be passed to target url so cross domain tracking could work.
Any help will be appreciated, thanks.
The _gaq.push(['_link', url]); function targets the current window with the 'url' you send it. It ignores target. What you need to do is call the Google function but then update the parent location.
Your page within the iFrame should look something link this
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx-xx']);
_gaq.push(['_setDomainName', 'example.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>
</head>
<body>
Link
<script>
function your_GA_track_link(url){
_gaq.push(['_link', url]);
self.parent.location.href = url;
return false;
}
</script>
</body>
</html>
I think I have found solution. Here is code that should be put in body:
<script type="text/javascript">
function openWindow(link) {
_gaq.push(function() {
var tracker = _gaq._getAsyncTracker(); //add name param if needed
window.open(tracker._getLinkerUrl(link.href),"_parent");
});
return false;
}
</script>
UPGRADE</br>

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>

jQuery to track Google analytics events not working

I'm trying to use google anayltics events but so far without any success..
What I'm doing is loading 5 pages using jQuery load function
and I want to track the "Next button" for each load. but looks like i'm doing something wrong..
This is the next button event code:
$('.NextButton').click(function () {
_gaq.push(['_trackEvent', 'fz_main_page', 'clicked']);
installation.load_page({ inner_page: next_page, comid: data.comid, outerid: data.outerid, single_app: "1" });
});
Analytics Code:
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-25162785-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>
What am I doing wrong?
It's possible that the installation.load_page function is preventing the trackEvent from firing. Try wrapping your load function in a setTimeout:
setTimeout('installation.load_page({ inner_page: next_page, comid: data.comid, outerid: data.outerid, single_app: "1" })', 100);
Install the Google Analytics Debugger. Look in the console (control, shift, j) for the event tracking processing.
If you don't see all of your events tracking there, then something else is up with the tracking code.

Tracking Multiple Domains with Google Analytics in same code

I have portal which behaves like multiple portal.
The portal code base is same but for all sites but content is based on which url or domain you came across.
Currently i am able to track my domain
example.com
and also subdomains like
a.example.com, b.example.com
. But i want to also track
anotherexample.com
which also point to same domain. How can I do?
Any suggestions?
multiple url pointed to same code base. It does not matter. The only matter is the url above and the tracking code in site is matching or not. So you have to take google access code from database which just write in address bar.
Codes:
<!-- Google Analytics -->
<script type="text/javascript">
var accountNameFromDB = ...Some Operations...
var domainNameFromDB = ...Some Operations...
var _gaq = _gaq || [];
_gaq.push(['_setAccount', accountNameFromDB]);
_gaq.push(['_setDomainName', domainNameFromDB]);
_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>
Just take accountNameFromDB and domainNameFromDB variables dynmically.
Add this code head of master page(or main template) or add every page.

Resources