Installation
Install the Package
npm
npm install @attributehq/web-sdkCDN (Script Tag)
Pin to a major version for automatic patch updates:
<script src="https://unpkg.com/@attributehq/web-sdk@1/dist/attribution.umd.js"></script>Or pin to an exact version:
<script src="https://cdn.jsdelivr.net/npm/@attributehq/web-sdk@1.0.0/dist/attribution.umd.js"></script>When using CDN, the SDK is available as window.AttributeHQ:
<script>
AttributeHQ.init({
apiKey: 'ak_your_api_key',
appId: 'your-app-uuid',
})
</script>Initialization
Call init() once, as early as possible. The SDK is a singleton — calling init() a second time is a no-op.
import { init } from '@attributehq/web-sdk'
init({
apiKey: 'ak_your_api_key',
appId: 'your-app-uuid',
})What Happens on Init
- Configuration is validated and defaults applied
- A new session is created (or resumed if within 30-minute window)
- If this is the user’s first visit (
__ahq_installednot in localStorage), the SDK sends an install request and stores the attribution result - If
autoTrackPageViewsis enabled (default), the initial page view is tracked and SPA navigation is monitored
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
apiKey | string | — | Required. API key from your dashboard (format: ak_xxx) |
appId | string | — | Required. App UUID from your dashboard |
batchSize | number | 10 | Number of events per batch before auto-flush |
flushInterval | number | 5000 | Time in ms between automatic flush attempts |
maxQueueSize | number | 100 | Maximum events stored in the offline queue |
autoTrackPageViews | boolean | true | Auto-track page views including SPA navigation |
debug | boolean | false | Log debug messages to console |
Framework Examples
Next.js / React
'use client'
import { useEffect } from 'react'
import { init } from '@attributehq/web-sdk'
export function AttributeHQProvider({ children }) {
useEffect(() => {
init({
apiKey: 'ak_your_api_key',
appId: 'your-app-uuid',
})
}, [])
return children
}Always initialize in a useEffect to ensure the SDK runs client-side only. The SDK requires window and localStorage.
Vanilla HTML
<!DOCTYPE html>
<html>
<head>
<script src="https://unpkg.com/@attributehq/web-sdk@1/dist/attribution.umd.js"></script>
</head>
<body>
<script>
AttributeHQ.init({
apiKey: 'ak_your_api_key',
appId: 'your-app-uuid',
})
</script>
</body>
</html>