Before using analytics.js
, you need to include the Google Analytics tracking code on your website.
Important: The analytics.js
library is deprecated. Consider migrating to gtag.js
for the latest features and support.
A quick reference guide to using the analytics.js library for Google Analytics, including setup, page tracking, event tracking, and custom dimensions. Note: analytics.js is deprecated, consider using gtag.js.
Before using Important: The |
Basic setup involves adding a script tag to your HTML:
|
Replace |
An alternative initialization method, potentially referencing a Measurement ID:
Note: This seems to be a hybrid approach more aligned with |
To track multiple properties, create multiple trackers with different names.
|
The most basic form of tracking is a page view. This is automatically triggered in the basic setup.
|
To track page views with a custom page path:
|
To track page views with a custom title:
|
Events are used to track user interactions with your content.
|
Example:
|
With a numeric value:
|
Note: |
Custom dimensions allow you to track data specific to your site.
Replace |
Example:
|
Or send it directly with the pageview:
|
Custom metrics are similar to dimensions, but they represent numerical data.
Replace |
Example:
|
The User ID feature allows you to track users across devices and sessions.
Replace |
Or during tracker creation:
|
You can manually start a new session:
|
Track exceptions (errors) that occur on your website.
|
Track how long it takes for certain tasks to complete (e.g., loading resources).
|
Note: Ecommerce tracking with |
The |