How to place Custom Variables in the new Google Analytics Code

扶醉桌前 提交于 2019-12-03 13:38:32
nt_1

You are indeed mixing up 2 incompatible 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.com/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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!