-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartners.cookie.html
134 lines (108 loc) · 4.04 KB
/
partners.cookie.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<script type="text/javascript">
/*
* Make sure this code is included at any page of your site before any output is started.
* The good way is to place it in the beginning of your index.php
*/
var settingsByPartner = {
cityads: {
pixelUrl: 'https://hskwq.com/service/newtrack/{orderId}/ct/{targetCode}/c/{campaignId}?click_id={clickId}'
}/*,
anotherCPA: {
pixelUrl: 'anotherUrl'
}
*/
};
function CPACounter(settingsByPartner) {
if (settingsByPartner && !CPACounter.settingsByPartner) {
CPACounter.settingsByPartner = settingsByPartner;
return;
} else if (CPACounter.settingsByPartner && !settingsByPartner) {
return;
}
throw new Error('CPACounter should be first instantiated with `settingsByPartner` argument, then it must be used with no arguments.');
}
CPACounter.prototype.setPartnersCookies = function (expirationDays) {
var queryParams = this.parseQueryParams(window.location.search);
if (!queryParams.utm_source) {
return;
}
for (var partnerName in CPACounter.settingsByPartner) {
if (CPACounter.settingsByPartner.hasOwnProperty(partnerName)) {
if (queryParams.utm_source === partnerName) {
var expirationDate = new Date();
expirationDate.setDate(expirationDate.getDate() + expirationDays);
this.setCookie(partnerName + 'ClickId', queryParams.utm_source || '', expirationDate, '/');
}
}
}
};
CPACounter.prototype.getPixelUrls = function (orderId, targetCode, partnerNames) {
if (!partnerNames) {
partnerNames = Object.keys(CPACounter.settingsByPartner);
}
var urls = [];
for (var i = 0; i < partnerNames.length; i++) {
var partnerName = partnerNames[i],
clickId = this.getCookie(partnerName + 'ClickId');
if (!clickId) {
continue;
}
urls.push(this.formatString(CPACounter.settingsByPartner[partnerName].pixelUrl, {
orderId: encodeURIComponent(orderId),
targetCode: targetCode,
campaignId: campaignId,
clickId: clickId,
}));
}
return urls;
};
CPACounter.prototype.insertPixels = function (orderId, targetCode, partnerNames) {
var urls = this.getPixelUrls(orderId, targetCode, partnerNames);
for (var i = 0; i < urls.length; i++) {
var url = urls[i];
var img = document.createElement('img');
img.style.display = 'none';
img.src = url;
document.body.appendChild(img);
}
};
CPACounter.prototype.setCookie = function (name, value, expires, path) {
document.cookie = encodeURIComponent(name) + '=' + encodeURIComponent(value) + '; path=' + path + '; expires=' + expires.toUTCString();
};
CPACounter.prototype.getCookie = function (name) {
let matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, '\\$1') + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : undefined;
};
CPACounter.prototype.formatString = function (string, data) {
return string.replace(/\{\s*(.*?)\s*\}/g, (i, match) => data.hasOwnProperty(match) ? data[match] : '');
};
CPACounter.prototype.parseQueryParams = (function () {
var re = /([^&=]+)=?([^&]*)/g;
var decodeRE = /\+/g; // Regex for replacing addition symbol with a space
var decode = function (str) { return decodeURIComponent( str.replace(decodeRE, " ") ); };
return function (query) {
var params = {}, e;
while (e = re.exec(query)) {
var k = decode( e[1] ), v = decode( e[2] );
if (k.substring(k.length - 2) === '[]') {
k = k.substring(0, k.length - 2);
(params[k] || (params[k] = [])).push(v);
} else {
params[k] = v;
}
}
return params;
};
})();
var cpaCounter = new CPACounter(settingsByPartner);
cpaCounter.setPartnersCookies();
</script>
<script type="text/javascript">
/*
* Place this code after the order is successfully done
*/
var cpaCounter = new CPACounter(); // $orderId is an ID of the order in your system. Contact integration team to talk about $targetCode
cpaCounter.insertPixels('orderId', 'targetCode');
</script>