# TikTok Pixel
The TikTok Pixel is a piece of JS code that you can place on your website that allows you to share website events with TikTok. With any of TikTok for Business Tools, the pixel can help measure traffic on your website, measure ad campaign performance, optimize your campaigns and find new customers.
This code is used for local landing pages loaded into the tracker. If there is no access to the code (offer redirect), you can use the Conversions API setting. See TikTok integration.
- Create traffic source in the tracker using a ready-made TikTok template and add the source to the campaign and the campaign parameters will be filled in automatically, including the
pixel
parameter:
Open the landing page in the Editor and go to
index.html
(orindex.php
).Add between
<head>
and</head>
that code:
<script>
var date = new Date();
date.setTime(date.getTime() + (5 * 24 * 60 * 60 * 1000));
if (!'{pixel}'.match('{')) {
document.cookie = "pixel={pixel}; " + "expires=" + date.toUTCString() + "";
}
</script>
2
3
4
5
6
7
WARNING
If another parameter in the campaign is used to transfer the pixel, for example, px
, you need to replace it in the script:
from document.cookie = "pixel={pixel}
to document.cookie = "pixel={px}
Example:
- Open the file or page where you want to install the pixel, for example, a thank you page, and add the following code:
<script>
var matches = document.cookie.match(new RegExp("(?:^|; )" + 'pixel' + "=([^;]*)"));
var pixel = matches ? decodeURIComponent(matches[1]) : undefined;
</script>
<!-- TikTok Pixel Code Start -->
<script>
!function (w, d, t) {
w.TiktokAnalyticsObject = t;
var ttq = w[t] = w[t] || [];
ttq.methods = ["page", "track", "identify", "instances", "debug", "on", "off", "once", "ready", "alias", "group", "enableCookie", "disableCookie"],
ttq.setAndDefer = function (t, e) {
t[e] = function () {
t.push([e].concat(Array.prototype.slice.call(arguments, 0)))
}
};
for (var i = 0; i < ttq.methods.length; i++) ttq.setAndDefer(ttq, ttq.methods[i]);
ttq.instance = function (t) {
for (var e = ttq._i[t] || [], n = 0; n < ttq.methods.length; n++) ttq.setAndDefer(e, ttq.methods[n]);
return e
}, ttq.load = function (e, n) {
var i = "https://analytics.tiktok.com/i18n/pixel/events.js";
ttq._i = ttq._i || {}, ttq._i[e] = [], ttq._i[e]._u = i, ttq._t = ttq._t || {}, ttq._t[e] = +new Date, ttq._o = ttq._o || {}, ttq._o[e] = n || {};
var o = document.createElement("script");
o.type = "text/javascript", o.async = !0, o.src = i + "?sdkid=" + e + "&lib=" + t;
var a = document.getElementsByTagName("script")[0];
a.parentNode.insertBefore(o, a)
};
ttq.load(pixel);
ttq.page();
}(window, document, 'ttq');
</script>
<!-- TikTok Pixel Code End -->
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
Example of a ready-made configuration option: