Last active
February 13, 2023 08:32
-
-
Save yuriy-yarvinen/bd8b5c935da191378fcc48e8096075fa to your computer and use it in GitHub Desktop.
getUtms
This file contains 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
function getUtms() { | |
const queryString = window.location.search; | |
const urlParams = new URLSearchParams(queryString); | |
const utm_source = urlParams.get('utm_source') ?? null; | |
const utm_medium = urlParams.get('utm_medium') ?? null; | |
const utm_campaign = urlParams.get('utm_campaign') ?? null; | |
const utm_content = urlParams.get('utm_content') ?? null; | |
const utm_term = urlParams.get('utm_term') ?? null; | |
return { | |
utm_source: utm_source, | |
utm_medium: utm_medium, | |
utm_campaign: utm_campaign, | |
utm_content: utm_content, | |
utm_term: utm_term | |
} | |
} | |
function getUrlWithUtms(url, utms) { | |
for (const key in utms) { | |
if (utms[key]) { | |
url = url.replace(key, utms[key]) | |
} | |
} | |
const urlArr = url.split("?"); | |
let newUrl = ''; | |
if(urlArr[0]){ | |
newUrl = urlArr[0]; | |
} | |
if(urlArr[1]){ | |
newUrl = newUrl + "?" + urlArr[1].replace(/[^A-Za-z0-9_=]/g, ""); | |
} | |
return newUrl; | |
} | |
let utmIntarvalCounter = 0; | |
let utms = getUtms(); | |
let needUrl = getUrlWithUtms('https://t.me/tgrealty_bot?start=utm_source__utm_medium__utm_campaign__utm_content__utm_term', utms); | |
let utmIntarval = setInterval(() => { | |
let linkObjets = document.querySelectorAll('a[href^="https://t.me/tgrealty_bot"]'); | |
if (linkObjets) { | |
if (linkObjets.length > 0) { | |
for (const key in linkObjets) { | |
if (linkObjets[key]) { | |
if (typeof linkObjets[key] == 'object') { | |
linkObjets[key].setAttribute('href', needUrl); | |
} | |
} | |
} | |
clearInterval(utmIntarval); | |
} | |
} | |
if (utmIntarvalCounter > 100) { | |
clearInterval(utmIntarval); | |
} | |
utmIntarvalCounter++; | |
}, 100); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment