# JS adapter
Keitaro doesn't support landing pages natively, so we must apply several changes. This JavaScript code is intended to work with URL parameters, cookies, and tracker elements on landing pages. That solution is compatible with:
- Local.
- External (Redirect).
- KClient PHP.
The code allows for the following actions:
- Configuring the transition from the landing page to the offer for local and external landings.
- Sending conversions to the tracker from your own site without an affiliate network using PHP or JavaScript code.
- Configuring the FB pixel on the main and internal pages of the landing.
# Setting up the code
For each page:
- Find
<head> .... </head>
tags. - Copy and paste that code:
<!-- JS adapter start -->
<script type="application/javascript">
function getCookie(name) {
var v = document.cookie.match("(^|;) ?" + name + "=([^;]*)(;|$)");
var value = v ? v[2] : null;
return value && value !== "undefined" ? value : null;
}
function setCookie(name, value, days) {
var d = new Date();
d.setTime(d.getTime() + 24 * 60 * 60 * 1000 * days);
document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
}
function getSubId() {
var params = new URLSearchParams(document.location.search.substr(1));
if (!"{subid}".match("{")) {
return "{subid}";
}
var clientSubid = '<?php echo isset($client) ? $client->getSubid() : "" ?>';
if (clientSubid && !clientSubid.match(">")) {
return clientSubid;
}
if (params.get("_subid")) {
return params.get("_subid");
}
if (params.get("subid")) {
return params.get("subid");
}
if (getCookie("subid")) {
return getCookie("subid");
}
if (getCookie("_subid")) {
return getCookie("_subid");
}
}
function getToken() {
var params = new URLSearchParams(document.location.search.substr(1));
if (!"{token}".match("{")) {
return "{token}";
}
var clientToken = '<?php echo isset($client) ? $client->getToken() : "" ?>';
if (clientToken && !clientToken.match(">")) {
return clientToken;
}
if (params.get("_token")) {
return params.get("_token");
}
if (params.get("token")) {
return params.get("token");
}
if (getCookie("token")) {
return getCookie("token");
}
return null;
}
function getPixel() {
var params = new URLSearchParams(document.location.search.substr(1));
if (!"{pixel}".match("{")) {
return "{pixel}";
}
if (params.get("pixel")) {
return params.get("pixel");
}
if (getCookie("pixel")) {
return getCookie("pixel");
}
return null;
}
if (typeof URLSearchParams === "function") {
document.addEventListener("DOMContentLoaded", function (event) {
var params = new URLSearchParams(document.location.search.substr(1));
var subid = getSubId();
var token = getToken();
var pixel = getPixel();
params.set("_token", token);
setCookie("pixel", pixel);
setCookie("token", token);
setCookie("subid", subid);
// Adds params to the links
document.querySelectorAll("a").forEach(function (link) {
try {
var url = new URL(link.href);
params.forEach(function (v, k) {
url.searchParams.append(k, v);
});
link.href = url.toString();
} catch (e) {
console.error(
`[Exception] Bad params: unexpected link '${link.href}' for new Url()`
);
}
});
var SUBID_TEMPLATE_NAME = "subid";
var TOKEN_TEMPLATE_NAME = "token";
var PIXEL_TEMPLATE_NAME = "pixel";
// Replace placeholders to actual values for input[hidden] fields
var subIdRegExp = new RegExp(`\{${SUBID_TEMPLATE_NAME}\}`, "g");
var tokenRegExp = new RegExp(`\{${TOKEN_TEMPLATE_NAME}\}`, "g");
var pixelRegExp = new RegExp(`\{${PIXEL_TEMPLATE_NAME}\}`, "g");
document
.querySelectorAll('input[type="hidden"]')
.forEach(function (input) {
if (subIdRegExp.test(input.value)) {
input.value = input.value.replaceAll(subIdRegExp, subid);
}
if (tokenRegExp.test(input.value)) {
input.value = input.value.replaceAll(tokenRegExp, token);
}
if (pixelRegExp.test(input.value)) {
input.value = input.value.replaceAll(pixelRegExp, pixel);
}
});
// Adds params as hidden inputs to the forms
document.querySelectorAll("form").forEach(function (form) {
params.forEach(function (v, k) {
var input = document.createElement("input");
input.type = "hidden";
input.name = k;
input.value = v;
form.append(input);
});
});
});
}
</script>
<!-- JS adapter end -->
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
135
136
137
138
139
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
135
136
137
138
139
# Creating an offer link
Set up JS Adapter on every page of the landing page.
Create offer link using the following constructions:
# Sending postback
- Set up JS Adapter in either
index.html
orindex.php
of your landing page. - Insert a postback sending code on thank_you_page (or on the handler of your landing page) using the following constructs:
- Replace the Postback URL with the URL of your tracker. See in Maintenance - Postback URL
- To receive conversion from remote offer, you must pass
subid={subid}
as offer parameter.
# FB Pixel
- Set up JS Adapter on all pages where you want to use FB pixel.
- Add the pixel code to the necessary pages. Use function
getPixel()
to get saved pixel value:
<!-- Facebook Pixel Code -->
<script>
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', getPixel());
fbq('track', 'Lead');
</script>
<!-- End Facebook Pixel Code -->
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
That pixel also works on any secondary page.
It's also compatible with solution mentioned on FB Pixel.