-
Notifications
You must be signed in to change notification settings - Fork 211
[PROD] - UTM register btn #7167
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
base: master
Are you sure you want to change the base?
Changes from all commits
0d44ccb
2eaecf7
42d1e9b
a495d36
1c26e03
c785a06
02378d1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // UTM cookie configuration constants | ||
| const TC_UTM_COOKIE_NAME = 'tc_utm'; | ||
|
|
||
| /** | ||
| * Retrieves and parses the tc_utm cookie | ||
| * @returns Parsed UTM parameters or null if cookie doesn't exist | ||
| */ | ||
| export function getUtmCookie() { | ||
| try { | ||
| const cookies = document.cookie.split(';'); | ||
| const cookieStr = cookies.find(cookie => cookie.trim().startsWith(`${TC_UTM_COOKIE_NAME}=`)); | ||
|
|
||
| if (!cookieStr) { | ||
| return null; | ||
| } | ||
|
|
||
| // handle values that might contain '=' | ||
| const cookieValue = decodeURIComponent(cookieStr.split('=').slice(1).join('=')); | ||
| return JSON.parse(cookieValue); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| } catch (error) { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Appends UTM parameters from the tc_utm cookie to a given URL | ||
| * Only appends parameters that exist in the cookie | ||
| * @param url - The base URL to append parameters to | ||
| * @returns URL with UTM parameters appended, or original URL if no cookie exists | ||
| */ | ||
| export function appendUtmParamsToUrl(url, defaultParams = {}) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| if (!url) { | ||
| return url; | ||
| } | ||
|
|
||
| const utmParams = getUtmCookie(); | ||
|
|
||
| // If there are no cookie params and no defaults, nothing to do | ||
| if ( | ||
| (!utmParams || Object.keys(utmParams).length === 0) | ||
| && (!defaultParams || Object.keys(defaultParams).length === 0) | ||
| ) { | ||
| return url; | ||
| } | ||
|
|
||
| try { | ||
| const urlObj = new URL(url, window.location.origin); | ||
| const paramNames = ['utm_source', 'utm_medium', 'utm_campaign']; | ||
|
|
||
| paramNames.forEach((param) => { | ||
| const cookieVal = utmParams && utmParams[param]; | ||
| const defaultVal = defaultParams && defaultParams[param]; | ||
|
|
||
| // Cookie takes precedence and will overwrite existing query param | ||
| if (cookieVal) { | ||
| urlObj.searchParams.set(param, cookieVal); | ||
| } else if (defaultVal) { | ||
| // Only apply default if the URL does not already have the param | ||
| if (!urlObj.searchParams.has(param)) { | ||
| urlObj.searchParams.set(param, defaultVal); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| return urlObj.toString(); | ||
| } catch (error) { | ||
| return url; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [ |
||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[❗❗
security]The
appendUtmParamsToUrlfunction is used to construct a URL with query parameters. Ensure that this function properly encodes all URL components to prevent potential security issues such as URL injection.