Skip to content

Instantly share code, notes, and snippets.

@TannerQuigley
Last active August 19, 2019 20:17
Show Gist options
  • Save TannerQuigley/3915bde69e6369faab903df9e084a3fc to your computer and use it in GitHub Desktop.
Save TannerQuigley/3915bde69e6369faab903df9e084a3fc to your computer and use it in GitHub Desktop.
const dbURL = 'https://magneta-mvp.herokuapp.com/users'
let brandUserID = '5d57078dd48b5a00177167d9';
let campaign;
const getUrlParams = () => {
const uurl = window.location.href;
let paramsObj = {};
let params = uurl.split('?')[1];
if (params) {
params = params.split('#')[0];
const paramsArr = params.split('&');
for (let i = 0; i < paramsArr.length; i++) {
let utmCombo = paramsArr[i]
let utmArr = utmCombo.split('=');
let tempObj = { [utmArr[0]]: utmArr[1] }
paramsObj = {
...paramsObj,
...tempObj
};
}
}
return paramsObj;
};
function setCookieObj(cobj, expDays) {
//set time to how many days to expire
const d = new Date();
d.setTime(d.getTime() + (expDays * 24 * 60 * 60 * 1000));
const expires = `expires = ${d.toUTCString()}`;
//Takes cookie object and then makes it a cookie
Object.keys(cobj).map((key) => {
document.cookie = `${key}= ${cobj[key]}; ${expires}; path=/;`
return null;
});
}
function setCoookie(cname, cval, expDays) {
const d = new Date();
d.setTime(d.getTime() + (expDays * 24 * 60 * 60 * 1000));
const expires = `expires = ${d.toUTCString()}`;
document.cookie = `${cname}= ${cval}; ${expires}; path=/`;
}
function getCookie(cname) {
const name = cname + "=";
const decodedCookie = decodeURIComponent(document.cookie);
const CookieArr = decodedCookie.split(';');
for (let i = 0; i < CookieArr.length; i++) {
let c = CookieArr[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length)
}
}
return "";
}
function initTracker() {
let userId = getCookie("userId");
//TODO: need to check if userId has already been tracked as well
if (!userId) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = () => {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
setCoookie("userId", xmlHttp.responseText, 30);
}
xmlHttp.open("GET", "https://www.uuidgenerator.net/api/version4 ", true); // false for synchronous request
xmlHttp.send(null);
let paramsObj = getUrlParams();
setCookieObj(paramsObj, 30);
}
// Get campaign info
campaign = getCampaign(brandUserID);
}
function Track(websiteID) {
initTracker();
// send to database as a click to the site
const clickReq = dbURL + '/updateClicks/' + paramsObj.campaign + '/' + paramsObj.channel + '/' + paramsObj.userId
postReq(reqURL, null)
// check database if equal to or begins with
let begins = campaign.destCond
// get request for get conversion location
const conLoc = campaign.conversionDest;
let winLocation = window.location.pathname;
console.log(winLocation)
if (winLocation === conLoc) {
//send to the database that there was conversions
const convReq = dbURL + '/updateConversions/' + paramsObj.campaign + '/' + paramsObj.channel + '/' + paramsObj.userId
postReq(convReq)
window.alert('your dad doesnt love you');
}
}
function getCampaign(userId) {
const url = 'https://magneta-mvp.herokuapp.com/users/' + userId
const xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", url, false); // false for synchronous request
xmlHttp.send(null);
return xmlHttp.responseText;
}
function postReq(reqURL, reqBody) {
const xmlHttp = new XMLHttpRequest();
xmlHttp.open("POST", reqURL, true); // false for synchronous request
xmlHttp.send(reqBody);
}
console.log(getCampaign(userId));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment