-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomScript.html
53 lines (52 loc) · 2.13 KB
/
customScript.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
<script id="p-offer-script">
function pmReplaceUrl(cep, queryFilled, callback) {
// Replace url
var cepData = {};
try {
if (cep) {
var cepDecode = atob(cep).split('').map(function(c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join('')
cepData = JSON.parse(decodeURIComponent(cepDecode).replace(/'/g, '"'))
}
} catch(e) {
cepData = {};
}
var urlParams = new URLSearchParams(window.location.search);
var isReplaceQuery = false;
urlParams.forEach(function(value, key) {
if ((value || '').match('{.+}|%7B.+%7D') && cepData[key]) {
isReplaceQuery = true;
urlParams.set(key, cepData[key]);
}
if (key === 'pm_cid' || key === 'pm_click_id' || key === 'pm_offer_id' || key === 'promo_code') {
urlParams.delete(key);
}
});
if (isReplaceQuery) {
callback(urlParams.toString());
window.history.replaceState({}, '', window.location.pathname + '?' + decodeURIComponent(urlParams.toString()));
return;
}
if (queryFilled) {
window.history.replaceState({}, '', window.location.href + (window.location.search ? '&' : '?') + decodeURIComponent(queryFilled));
}
}
function pmSendPixel(url, queryFilled, callback, queryCallback) {
fetch(url)
.then(function (result) {return result.text()})
.then(function(text) {
var cep = '';
var click_id = '';
var campaign = '';
var cepMatch = (text || '').match(/cep\s*:\s*"([^"]+)"/);
var clickIdMatch = (text || '').match(/click_id\s*:\s*"([^"]+)"/);
var campaignMatch = (text || '').match(/campaign\s*:\s*"([^"]+)"/);
if (cepMatch && cepMatch[1]) cep = cepMatch[1];
if (clickIdMatch && clickIdMatch[1]) click_id = clickIdMatch[1];
if (campaignMatch && campaignMatch[1]) campaign = campaignMatch[1];
var query = pmReplaceUrl(cep, queryFilled, queryCallback);
callback({cep: cep, click_id: click_id, campaign: campaign});
});
}
</script>