Script Tag/Web
The Alytica Web SDK provides a simple way to track user behavior on your website by embedding a script tag. This document details the installation process and explains how to use the Web SDK within your project.
Overview
The Alytica JavaScript SDK allows you to track user interactions, page views, web vitals, and custom events on your website. This documentation covers all available methods and configuration options.
Installation
Direct Script Installation
Add the following code to the <head>
section of your HTML:
Configuration Options
When initializing the SDK, you can provide several configuration options:
Option | Type | Default | Description |
---|---|---|---|
clientId | String | Required | Your Alytica Client ID |
clientSecret | String | Optional | Your Alytica Client Secret |
trackPageViews | Boolean | false | Automatically track page views |
trackWebVitals | Boolean | false | Track Web Vitals metrics |
trackOutgoingLinks | Boolean | false | Track clicks on external links |
trackAttributes | Boolean | false | Track elements with data-track attributes |
trackHashChanges | Boolean | false | Track hash changes as page views |
debug | Boolean | false | Enable debug mode |
disabled | Boolean | false | Disable tracking entirely |
waitForProfile | Boolean | true | Queue events until ready() is called |
processProfiles | Boolean | true | Process user profiles on the server |
Core Methods
Track Events
Identify Users
Create Aliases
Get Distinct ID
Reset User
Resets the current user and generates a new anonymous ID:
Set Global Properties
Add properties to all subsequent events:
Manual Page View
Manually track a page view:
Ready
Process queued events when tracking is initially configured with waitForProfile: true
:
Automatic Tracking
Web Vitals Tracking
When trackWebVitals: true
is set, the SDK automatically tracks:
- Cumulative Layout Shift (CLS)
- Largest Contentful Paint (LCP)
- First Contentful Paint (FCP)
- Interaction to Next Paint (INP)
Events are sent with the name $web_vitals
and include:
$metric_name
: The name of the metric$metric_value
: The value of the metric$metric_rating
: The performance rating (good, needs improvement, poor)$metric_delta
: The change in the metric value
Page View Tracking
When trackPageViews: true
is set, page views are tracked automatically on:
- Initial page load
- History API changes (
pushState
,replaceState
) popstate
events- Hash changes (if
trackHashChanges: true
)
Events are sent with the name $pageview
and include:
$path
: Current URL$title
: Page title- Other global properties
Outbound Link Tracking
When trackOutgoingLinks: true
is set, clicks on external links are tracked automatically.
Events are sent with the name $linkOut
and include:
href
: The link destinationtext
: The link text or title
Data Attribute Tracking
When trackAttributes: true
is set, clicks on elements with data-track
attributes are tracked automatically:
This generates an event named signup_click
with properties:
plan
: "premium"source
: "homepage"
Cookie Management
The SDK uses cookies to store user and session information:
- Cookie name format:
alytica_[clientId]
- Default expiration: 365 days
- Contains:
- Distinct ID
- Session information
- Initial user properties
Session Management
- Sessions expire after 30 minutes of inactivity
- New sessions generate events with updated session IDs
- Event counts are maintained per session
Debugging
When debug: true
is set:
- Events are logged to the console
- The Alytica logo is displayed in the console
Browser Compatibility
The SDK is compatible with all modern browsers that support:
fetch
APIPromise
crypto.randomUUID()
- ES6 features