The analytics.js JavaScript snippet is a new way to measure how users interact with your website. It is similar to the previous tracking code, ga.js, but offers more flexibility for developers to customize their implementations.
Tracking Code Quick Start
To begin tracking a website using analytics.js, paste the following JavaScript snippet into
your website template page so that it appears before the
closing </head>
tag. The UA-XXXX-Y
, parameter
must be replaced with the Web Property ID for the Google Web Property
you wish to track.
<!-- Google 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-XXXX-Y'); ga('send', 'pageview'); </script> <!-- End Google Analytics -->
When the code runs, it asynchronously loads the Google Analytics Tracking
Code onto the page. It then creates a tracker object for the web property
you specified in the UA-XXXX-Y
parameter. Finally, it records
a pageview in Google Analytics for the page that the script was run.
What Next
-
Advanced Configuration
Customize your implementation using the rich set of anlaytics.js APIs.
-
Page Tracking
Modify the default pages Google Analytics measures.
-
Event Tracking
Go beyond pageviews. Measure how users interact with web content.
-
Ecommerce Tracking
Collect ecommerce data with Google Analytics to measure true return on investment.
-
Social Interaction
Measure clicks and interactions with social buttons and plugins.
-
User Timings
Measure the time it takes for resources to load with this generic time measurement.
-
Custom Dimensions & Metrics
Level up. Send custom business data directly to Google Analytics.
-
Domains & Cookies
Understand the implications of multi-domain implementations.
References
-
Method Reference
Details of the objects and methods exposed by the analytics.js library.
-
Field Reference
The comprehensive list of all configuration fields for the analytics.js library.