Catalog / Google Analytics (analytics.js) Cheatsheet
Google Analytics (analytics.js) Cheatsheet
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.
Basic Setup & Configuration
Initialization
Before using Important: The |
Basic setup involves adding a script tag to your HTML:
|
Replace |
Alternative Initialization
An alternative initialization method, potentially referencing a Measurement ID:
Note: This seems to be a hybrid approach more aligned with |
Tracking Multiple Properties
To track multiple properties, create multiple trackers with different names.
|
Page and Event Tracking
Page Tracking
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:
|
Event Tracking
Events are used to track user interactions with your content.
|
Example:
|
With a numeric value:
|
Note: |
Custom Dimensions & Metrics
Setting Custom Dimensions
Custom dimensions allow you to track data specific to your site.
Replace |
Example:
|
Or send it directly with the pageview:
|
Setting Custom Metrics
Custom metrics are similar to dimensions, but they represent numerical data.
Replace |
Example:
|
User and Session Management
Setting User ID
The User ID feature allows you to track users across devices and sessions.
Replace |
Or during tracker creation:
|
Session Control
You can manually start a new session:
|
Advanced Features & Considerations
Exception Tracking
Track exceptions (errors) that occur on your website.
|
Timing
Track how long it takes for certain tasks to complete (e.g., loading resources).
|
Ecommerce Tracking
Note: Ecommerce tracking with |
Important Considerations (Deprecation)
The |