Reputation: 573
Is it possible to register a custom var in a google site? I've been trying for a week and still can't get it.
I know there are two ways to do this.
Async: this is recommended by analytics docs, but it doesn't work because gSites doesn't allow to add a node dynamically using DOM. I tried a lot of times...
_gaq.push(['_setCustomVar', 4, 'customVar4', "cuatro", 2 ]);
_gaq.push(['_trackPageview']);
Sync: I can't make this work, it tries to use _gat object before it is ready (or fully loaded) and end with an error.
var tracker = _gat._getTracker("UA-XXXXXX-X");
tracker._setCustomVar(2,"tracker2","mail",1);
pageTracker._trackPageview();
Can anybody help me? the problem is in google sites only, creating an app script, and adding it as a gadget.
Thanks.
Upvotes: 3
Views: 300
Reputation: 1275
Since Google Sites doesn't allow dynamic insertion, just add the script explicitly:
<script type="text/javascript" src="https://ssl.google-analytics.com/ga.js"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-XXXXX-X']);
_gaq.push(['_setCustomVar', 4, 'customVar4', "cuatro", 2 ]);
_gaq.push(['_trackPageview']);
</script>
Upvotes: 1
Reputation: 3216
As described in this help page
<script>
tagTry to add the common code snippet:
<script type="text/javascript">
_gaq.push(function() {
var tracker = _gat._getTrackerByName();
tracker._setCustomVar(1, 'Feedback', 'Closed', 1);
tracker._trackPageview();
});
</script>
I also recommend you to check the Google Analytic's devguide.
Upvotes: 3
Reputation: 686
<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>
https://developers.google.com/analytics/devguides/collection/gajs/asyncTracking
Upvotes: 2