-
Notifications
You must be signed in to change notification settings - Fork 211
[PROD] - UTM register btn #7168
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
Changes from 4 commits
5be128c
30d0fd2
cc6a1de
abb51ca
a39ab03
de87008
411eb50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| 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 = {}) { | ||
| 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 the URL with UTM parameters. Ensure that this function properly encodes all URL components to prevent any potential URL injection vulnerabilities.