@vincentt-sdks/campaign-sdk
Version:
Campaign SDK by Vincentt
36 lines (35 loc) • 1.16 kB
JavaScript
/**
* Parse UTM parameters from the current URL's query string
* @returns UTMParams object with parsed values, or undefined if no UTM params found
*/
export const parseUTMFromURL = () => {
if (typeof window === 'undefined') {
return undefined;
}
const urlParams = new URLSearchParams(window.location.search);
const utmSource = urlParams.get('utm_source');
const utmMedium = urlParams.get('utm_medium');
const utmCampaign = urlParams.get('utm_campaign');
const utmTerm = urlParams.get('utm_term');
const utmContent = urlParams.get('utm_content');
if (!utmSource && !utmMedium && !utmCampaign && !utmTerm && !utmContent) {
return undefined;
}
return {
source: utmSource || undefined,
medium: utmMedium || undefined,
campaign: utmCampaign || undefined,
term: utmTerm || undefined,
content: utmContent || undefined,
};
};
/**
* Get the document referrer
* @returns referrer URL or undefined
*/
export const getReferrer = () => {
if (typeof document === 'undefined') {
return undefined;
}
return document.referrer || undefined;
};