A lightweight, configurable JavaScript utility for capturing and storing marketing attribution parameters, ad platform tracking IDs, and referrer information. This tracker respects user privacy settings and provides flexible storage options.
Marketing attribution can be complex, especially when dealing with multiple advertising platforms and traffic sources. This utility:
- Automatically captures UTM parameters from your marketing campaigns
- Tracks ad platform-specific parameters (Facebook, Google Ads, Twitter, Reddit, Meta)
- Stores the initial HTTP referrer and complete landing page URL
- Respects user privacy settings and GDPR compliance
- Provides flexible storage options (cookies or sessionStorage)
- Offers easy access to stored attribution data
- Works with any website or application
- Requires no external dependencies
// Include the AttributionTracker class in your project
// Initialize it when your application loads
const tracker = new AttributionTracker();
The tracker accepts a configuration object with the following options:
const tracker = new AttributionTracker({
cookieDuration: 30, // Number of days to store data (default: 30)
useSessionStorage: false, // Use sessionStorage instead of cookies (default: false)
additionalParams: [], // Array of additional URL parameters to track
storageKey: 'attribution_data' // Key used for storage (default: 'attribution_data')
});
Option | Type | Default | Description |
---|---|---|---|
cookieDuration | number | 30 | Number of days before the cookie expires |
useSessionStorage | boolean | false | If true, uses sessionStorage instead of cookies |
additionalParams | string[] | [] | Additional URL parameters to track |
storageKey | string | 'attribution_data' | Key used for cookie or sessionStorage |
- utm_source
- utm_medium
- utm_campaign
- utm_content
- utm_term
- fbclid
- fb_source
- fb_ref
- mclid
- gclid
- gclsrc
- dclid
- gad_source
- twclid
- tw_source
- rdt_cid
- rdt_source
Returns all stored attribution data as an object.
const allData = tracker.getAll();
Returns only UTM parameters from stored data.
const utmData = tracker.getUtmParameters();
Returns only ad platform-specific parameters.
const adData = tracker.getAdPlatformParameters();
Returns the initial HTTP referrer URL.
const referrer = tracker.getReferrer();
Returns the full landing page URL where tracking began.
const landingPage = tracker.getLandingPage();
Returns the timestamp when the attribution data was captured.
const timestamp = tracker.getTimestamp();
Clears all stored attribution data.
tracker.clear();
The included cookie consent checker is a generic implementation that may need to be modified based on your specific Consent Management Platform (CMP). The current implementation:
- Checks for a global
window.cookieConsent
variable - Looks for common consent cookies
- Defaults to false if no consent is found
You should modify the checkCookieConsent()
method to integrate with your specific CMP. Example implementations:
// OneTrust example
checkCookieConsent() {
return OnetrustActiveGroups.includes('C0002');
}
// Cookiebot example
checkCookieConsent() {
return Cookiebot.consent.marketing;
}
// Custom implementation
checkCookieConsent() {
return yourConsentFunction();
}
// Initialize tracker
const tracker = new AttributionTracker();
// Later, when you need the data
const allAttributionData = tracker.getAll();
console.log('Attribution Data:', allAttributionData);
// Initialize with custom settings
const tracker = new AttributionTracker({
cookieDuration: 60,
useSessionStorage: true,
additionalParams: ['affiliate_id', 'custom_source'],
storageKey: 'my_attribution_data'
});
// Get specific data types
const utmData = tracker.getUtmParameters();
const adData = tracker.getAdPlatformParameters();
const referrer = tracker.getReferrer();
// Initialize tracker
const tracker = new AttributionTracker();
// When processing an order
function processOrder(orderData) {
const attributionData = tracker.getAll();
// Combine order and attribution data
const enrichedOrderData = {
...orderData,
attribution: attributionData
};
// Send to your analytics or order processing system
sendToAnalytics(enrichedOrderData);
}
- Initialize the tracker as early as possible in your application lifecycle
- Customize the cookie consent checker for your specific CMP
- Consider using sessionStorage for shorter user sessions
- Clear old attribution data when appropriate (e.g., after conversion)
- Regularly check stored data format and validity
The tracker uses standard web APIs and is compatible with all modern browsers. Key requirements:
URLSearchParams
APIsessionStorage
API- JSON parsing/stringifying
- Cookie handling
Feel free to submit issues and enhancement requests.
MIT License - feel free to use this in your projects.