I wanna place Custom Variables in Google Analytics but Ima little bit confused about the syntax.
This is what Google gave me to place on my site:-
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics/analytics.js','ga');
ga('create', 'UA-xxxxxxx', 'xxxxx');
ga('send', 'pageview');
This is what I want to use for Custom Vars:-
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx-XX']);
_gaq.push(['_setCustomVar', 1, 'age', '<?php echo $_GET["age"]; ?>', 1]);
_gaq.push(['_setCustomVar', 2, 'gender', '<?php echo $_GET["gender"];?>', 1]);
Now the example I saw says that I've to put the Custom Variables code as :-
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx-XX']);
_gaq.push(['_setCustomVar', 1, 'age', '<?php echo $_GET["age"]; ?>', 1]);
_gaq.push(['_setCustomVar', 2, 'gender', '<?php echo $_GET["gender"];?>', 1]);
_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/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
but it seems that the last three lines are old way of how google did page views, what google now gives (the first posted code) is how google does it now. Do you think it is correct? or should I just paste this code on top of what google gave me?
So, in short is this correct Google Analytics code, for me to add a custom Variables?
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx-XX']);
_gaq.push(['_setCustomVar', 1, 'age', '<?php echo $_GET["age"]; ?>', 1]);
_gaq.push(['_setCustomVar', 2, 'gender', '<?php echo $_GET["gender"];?>', 1]);
_gaq.push(['_trackPageview']);
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics/analytics.js','ga');
ga('create', 'UA-xxxxxxx', 'xxxxx');
ga('send', 'pageview');
I wanna place Custom Variables in Google Analytics but Ima little bit confused about the syntax.
This is what Google gave me to place on my site:-
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics./analytics.js','ga');
ga('create', 'UA-xxxxxxx', 'xxxxx.');
ga('send', 'pageview');
This is what I want to use for Custom Vars:-
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx-XX']);
_gaq.push(['_setCustomVar', 1, 'age', '<?php echo $_GET["age"]; ?>', 1]);
_gaq.push(['_setCustomVar', 2, 'gender', '<?php echo $_GET["gender"];?>', 1]);
Now the example I saw says that I've to put the Custom Variables code as :-
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx-XX']);
_gaq.push(['_setCustomVar', 1, 'age', '<?php echo $_GET["age"]; ?>', 1]);
_gaq.push(['_setCustomVar', 2, 'gender', '<?php echo $_GET["gender"];?>', 1]);
_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./ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
but it seems that the last three lines are old way of how google did page views, what google now gives (the first posted code) is how google does it now. Do you think it is correct? or should I just paste this code on top of what google gave me?
So, in short is this correct Google Analytics code, for me to add a custom Variables?
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-xxxxxx-XX']);
_gaq.push(['_setCustomVar', 1, 'age', '<?php echo $_GET["age"]; ?>', 1]);
_gaq.push(['_setCustomVar', 2, 'gender', '<?php echo $_GET["gender"];?>', 1]);
_gaq.push(['_trackPageview']);
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics./analytics.js','ga');
ga('create', 'UA-xxxxxxx', 'xxxxx.');
ga('send', 'pageview');
Share
Improve this question
edited Jun 26, 2013 at 1:33
Steve
asked Jun 26, 2013 at 1:05
SteveSteve
2,5889 gold badges51 silver badges94 bronze badges
2
-
2
You have opened yourself up to XSS attacks. Any variable data used on the context of JavaScript should be ran through
json_encode()
first! Do not blindly insert user-defined values in your code like this. – Brad Commented Jun 26, 2013 at 1:36 - Thanks Brad. 'll fix this up. – Steve Commented Jun 26, 2013 at 17:02
1 Answer
Reset to default 15You are indeed mixing up 2 inpatible Google Analytics libraries - ga.js and analytics.js.
Custom Variables as such do not exist in the analytics.js library and you should use Custom dimensions instead. If you define your age and gender variables in Javascript, you can then use the following call to pass them along with a pageview :
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics./analytics.js','ga');
ga('create', 'UA-XXXX-Y');
ga('send', 'pageview', {
'dimension1': age,
'dimension2': gender
});
</script>
Scope (hit/visit/visitor) and variable name are defined in the Google Analytics Custom Dimension interface - not in your code.