-
Couldn't load subscription status.
- Fork 12
feat: support persistent UTM targeting #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+492
−48
Merged
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0164a28
feat: support persistent UTM targeting
tyiuhc 368d658
simplify tests
tyiuhc 7ae02ad
update with dedicated user prop
tyiuhc a9cefec
fix tests
tyiuhc e6bcaae
fix campaign test suite
tyiuhc c697540
Update packages/experiment-tag/src/util/campaign.ts
tyiuhc b8eb922
simplify tests, use UTMParameter typing, add js doc
tyiuhc d560b13
Merge branch 'web/campaign-targeting' of github.com:amplitude/experim…
tyiuhc c56a4d0
add js doc
tyiuhc 8b8377c
add typing to campaign.ts imports
tyiuhc 92bc286
update to persisted_url_param
tyiuhc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { | ||
| type Campaign, | ||
| CampaignParser, | ||
| CookieStorage, | ||
| getStorageKey, | ||
| MKTG, | ||
| } from '@amplitude/analytics-core'; | ||
| import { UTMParameters } from '@amplitude/analytics-core/lib/esm/types/campaign'; | ||
| import { type ExperimentUser } from '@amplitude/experiment-js-client'; | ||
|
|
||
| import { getStorageItem, setStorageItem } from './storage'; | ||
|
|
||
| /** | ||
| * Enriches the user object's userProperties with UTM parameters based on priority: | ||
| * 1. URL params (highest priority) | ||
| * 2. experiment-tag persisted props (medium priority) | ||
| * 3. analytics-browser persisted props (lowest priority, if using default Amplitude Analytics integration) | ||
| */ | ||
| export async function enrichUserWithCampaignData( | ||
| apiKey: string, | ||
| user: ExperimentUser, | ||
| ): Promise<ExperimentUser> { | ||
| const experimentStorageKey = `EXP_${MKTG}_${apiKey.substring(0, 10)}`; | ||
| const [currentCampaign, persistedAmplitudeCampaign] = await fetchCampaignData( | ||
| apiKey, | ||
| ); | ||
| const persistedExperimentCampaign = getStorageItem<UTMParameters>( | ||
| 'localStorage', | ||
| experimentStorageKey, | ||
| ); | ||
|
|
||
| // Filter out undefined values and non-UTM parameters | ||
| const utmParams: Partial<UTMParameters> = {}; | ||
| const allCampaigns = [ | ||
| persistedAmplitudeCampaign, // lowest priority | ||
| persistedExperimentCampaign, // medium prioirty | ||
| currentCampaign, // highest priority | ||
| ]; | ||
|
|
||
| for (const campaign of allCampaigns) { | ||
| if (campaign) { | ||
| for (const [key, value] of Object.entries(campaign)) { | ||
| if (key.startsWith('utm_') && value !== undefined) { | ||
| utmParams[key] = value; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (Object.keys(utmParams).length > 0) { | ||
| persistUrlUtmParams(apiKey, utmParams); | ||
| return { | ||
| ...user, | ||
| persisted_utm_param: utmParams, | ||
tyiuhc marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
| } | ||
| return user; | ||
| } | ||
|
|
||
| /** | ||
| * Persists UTM parameters from the current URL to experiment-tag storage | ||
| */ | ||
| export function persistUrlUtmParams( | ||
| apiKey: string, | ||
| campaign: Record<string, string>, | ||
| ): void { | ||
| const experimentStorageKey = `EXP_${MKTG}_${apiKey.substring(0, 10)}`; | ||
| setStorageItem('localStorage', experimentStorageKey, campaign); | ||
| } | ||
|
|
||
| async function fetchCampaignData( | ||
| apiKey: string, | ||
| ): Promise<[Campaign, Campaign | undefined]> { | ||
| const storage = new CookieStorage<Campaign>(); | ||
| const storageKey = getStorageKey(apiKey, MKTG); | ||
| const currentCampaign = await new CampaignParser().parse(); | ||
| const previousCampaign = await storage.get(storageKey); | ||
| return [currentCampaign, previousCampaign]; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.