GoogleAnalytics
Questions answered by this recipe
- How can I add Google Analytics tracking code to my site?
Description
Adding the tracking code for Google Analytics is easy -- simply add the following into any local or group customization file, replacing the UA-CODE-1
with your Google Analytics tracking code:
(To have the analytics code at the end of the page instead of the beginning, use $HTMLFooterFmt
instead of $HTMLHeaderFmt
. -- Pm)
$HTMLHeaderFmt['analytics'] = " <script src='http://www.google-analytics.com/urchin.js' type='text/javascript'></script> <script type='text/javascript'> _uacct = 'UA-CODE-1'; </script>";
New Google Analytics code (may 2015 ; replace UA-××××××-×
)
$HTMLHeaderFmt['analytics'] = " <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-××××××-×', 'auto'); ga('send', 'pageview'); </script>";
Other Method
You could also just copy the code into the .tmpl file of your standard skin.
Notes
It's also possible to add the above <script>
lines directly into a skin template file, but a site should only do one or the other.
See Also
Contributors
Comments
See discussion at GoogleAnalytics-Talk
User notes? : If you use, used or reviewed this recipe, you can add your name. These statistics appear in the Cookbook listings and will help newcomers browsing through the wiki.