# JS Adapter

Keitaro doesn't support natively landing pages, so we must to apply several changes.

That solution is compatible with:

# Setting up the code

For each page:

  1. Find <head> .... </head> tags.
  2. Copy and paste that code:
<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) {
        var url = new URL(link.href); 
        params.forEach(function(v, k) { 
            url.searchParams.append(k, v);
        }); 
        link.href = url.toString();
    });

    // Replace placeholders to actual values for input[hidden] fields
    const subIdRegExp = new RegExp('\{subid\}', 'g');
    const tokenRegExp = new RegExp('\{token\}', 'g');
    const pixelRegExp = new RegExp('\{pixel\}', '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>
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

Read also Offer URL page.

# Sending postback

# FB Pixel

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

That pixel works on any secondary page as well.

It's also compatible with solution mentioned on FB Pixel.