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', 'auto');
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.
-
App / Screen Tracking
Measure how users interact with your app.
-
Cross Domain Tracking
Measure users across domains.
-
Display Features
Enable features such as Remarketing, Demographics and Interest Reporting, and more.
-
Custom Dimensions & Metrics
Level up. Send custom business data directly to Google Analytics.
-
Domains & Cookies
Understand the implications of multi-domain implementations.
-
Ecommerce Tracking
Collect ecommerce data with Google Analytics to measure true return on investment.
-
Event Tracking
Go beyond pageviews. Measure how users interact with web content.
-
Exception Tracking
Measure the number and type of crashes and exceptions that occur in your app.
-
Experiments
Test changes to your property to see how they perform in optimizing for a specific goal.
-
Page Tracking
Modify the default pages Google Analytics measures.
-
Social Interaction
Measure clicks and interactions with social buttons and plugins.
-
User ID
Enable the analysis of groups of sessions, across devices.
-
User Timings
Measure the time it takes for resources to load with this generic time measurement.
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.