|
| 1 | +import { |
| 2 | + type Campaign, |
| 3 | + CampaignParser, |
| 4 | + CookieStorage, |
| 5 | + getStorageKey, |
| 6 | + MKTG, |
| 7 | +} from '@amplitude/analytics-core'; |
| 8 | +import { UTMParameters } from '@amplitude/analytics-core/lib/esm/types/campaign'; |
| 9 | +import { type ExperimentUser } from '@amplitude/experiment-js-client'; |
| 10 | + |
| 11 | +import { getStorageItem, setStorageItem } from './storage'; |
| 12 | + |
| 13 | +/** |
| 14 | + * Enriches the user object's userProperties with UTM parameters based on priority: |
| 15 | + * 1. URL params (highest priority) |
| 16 | + * 2. experiment-tag persisted props (medium priority) |
| 17 | + * 3. analytics-browser persisted props (lowest priority, if using default Amplitude Analytics integration) |
| 18 | + */ |
| 19 | +export async function enrichUserWithCampaignData( |
| 20 | + apiKey: string, |
| 21 | + user: ExperimentUser, |
| 22 | +): Promise<ExperimentUser> { |
| 23 | + const experimentStorageKey = `EXP_${MKTG}_${apiKey.substring(0, 10)}`; |
| 24 | + const [currentCampaign, persistedAmplitudeCampaign] = await fetchCampaignData( |
| 25 | + apiKey, |
| 26 | + ); |
| 27 | + const persistedExperimentCampaign = getStorageItem<UTMParameters>( |
| 28 | + 'localStorage', |
| 29 | + experimentStorageKey, |
| 30 | + ); |
| 31 | + |
| 32 | + // Filter out undefined values and non-UTM parameters |
| 33 | + const utmParams: Partial<UTMParameters> = {}; |
| 34 | + const allCampaigns = [ |
| 35 | + persistedAmplitudeCampaign, // lowest priority |
| 36 | + persistedExperimentCampaign, // medium prioirty |
| 37 | + currentCampaign, // highest priority |
| 38 | + ]; |
| 39 | + |
| 40 | + for (const campaign of allCampaigns) { |
| 41 | + if (campaign) { |
| 42 | + for (const [key, value] of Object.entries(campaign)) { |
| 43 | + if (key.startsWith('utm_') && value !== undefined) { |
| 44 | + utmParams[key] = value; |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if (Object.keys(utmParams).length > 0) { |
| 51 | + persistUrlParams(apiKey, utmParams); |
| 52 | + return { |
| 53 | + ...user, |
| 54 | + persisted_url_param: utmParams, |
| 55 | + }; |
| 56 | + } |
| 57 | + return user; |
| 58 | +} |
| 59 | + |
| 60 | +/** |
| 61 | + * Persists UTM parameters from the current URL to experiment-tag storage |
| 62 | + */ |
| 63 | +export function persistUrlParams( |
| 64 | + apiKey: string, |
| 65 | + campaign: Record<string, string>, |
| 66 | +): void { |
| 67 | + const experimentStorageKey = `EXP_${MKTG}_${apiKey.substring(0, 10)}`; |
| 68 | + setStorageItem('localStorage', experimentStorageKey, campaign); |
| 69 | +} |
| 70 | + |
| 71 | +async function fetchCampaignData( |
| 72 | + apiKey: string, |
| 73 | +): Promise<[Campaign, Campaign | undefined]> { |
| 74 | + const storage = new CookieStorage<Campaign>(); |
| 75 | + const storageKey = getStorageKey(apiKey, MKTG); |
| 76 | + const currentCampaign = await new CampaignParser().parse(); |
| 77 | + const previousCampaign = await storage.get(storageKey); |
| 78 | + return [currentCampaign, previousCampaign]; |
| 79 | +} |
0 commit comments