@blotoutio/providers-tiktok-sdk 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -0
- package/index.js +188 -0
- package/package.json +21 -0
package/README.md
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
var ProvidersTiktokSdk = (function () {
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
/* eslint-enable */
|
|
5
|
+
const snippet = () => {
|
|
6
|
+
if (!window || !document || window.ttq) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
window.TiktokAnalyticsObject = 'ttq';
|
|
10
|
+
const ttq = (window['ttq'] =
|
|
11
|
+
window['ttq'] || []);
|
|
12
|
+
ttq.methods = [
|
|
13
|
+
'page',
|
|
14
|
+
'track',
|
|
15
|
+
'identify',
|
|
16
|
+
'instances',
|
|
17
|
+
'debug',
|
|
18
|
+
'on',
|
|
19
|
+
'off',
|
|
20
|
+
'once',
|
|
21
|
+
'ready',
|
|
22
|
+
'alias',
|
|
23
|
+
'group',
|
|
24
|
+
'enableCookie',
|
|
25
|
+
'disableCookie',
|
|
26
|
+
];
|
|
27
|
+
ttq.setAndDefer = function (t, e) {
|
|
28
|
+
t[e] = function () {
|
|
29
|
+
// eslint-disable-next-line
|
|
30
|
+
t.push([e].concat(Array.prototype.slice.call(arguments, 0)));
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
for (let i = 0; i < ttq.methods.length; i++) {
|
|
34
|
+
ttq.setAndDefer(ttq, ttq.methods[i]);
|
|
35
|
+
}
|
|
36
|
+
ttq.instance = function (t) {
|
|
37
|
+
const e = ttq._i[t] || [];
|
|
38
|
+
for (let n = 0; n < ttq.methods.length; n++) {
|
|
39
|
+
ttq.setAndDefer(e, ttq.methods[n]);
|
|
40
|
+
}
|
|
41
|
+
return e;
|
|
42
|
+
};
|
|
43
|
+
ttq.load = function (e, n) {
|
|
44
|
+
const i = 'https://analytics.tiktok.com/i18n/pixel/events.js';
|
|
45
|
+
ttq._i = ttq._i || {};
|
|
46
|
+
ttq._i[e] = [];
|
|
47
|
+
ttq._i[e]['_u'] = i;
|
|
48
|
+
ttq._t = ttq._t || {};
|
|
49
|
+
ttq._t[e] = +new Date();
|
|
50
|
+
ttq._o = ttq._o || {};
|
|
51
|
+
ttq._o[e] = n || {};
|
|
52
|
+
const element = document.createElement('script');
|
|
53
|
+
element.type = 'text/javascript';
|
|
54
|
+
element.async = !0;
|
|
55
|
+
element.src = i + '?sdkid=' + e + '&lib=ttq';
|
|
56
|
+
const script = document.getElementsByTagName('script')[0];
|
|
57
|
+
if (script && script.parentNode) {
|
|
58
|
+
script.parentNode.insertBefore(element, script);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
};
|
|
62
|
+
const init = ({ manifest, userId }) => {
|
|
63
|
+
if (!manifest.variables || manifest.variables['enableBrowser'] !== '1') {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const firstLoad = !window.ttq;
|
|
67
|
+
snippet();
|
|
68
|
+
if (!firstLoad || !window.ttq) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
window.ttq.load(manifest.variables['pixelId']);
|
|
72
|
+
window.ttq.instance(manifest.variables['pixelId']).identify({
|
|
73
|
+
external_id: userId,
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const getEventName = (event) => {
|
|
78
|
+
if (event === 'PageView') {
|
|
79
|
+
return 'ViewContent';
|
|
80
|
+
}
|
|
81
|
+
if (event === 'Purchase') {
|
|
82
|
+
return 'PlaceAnOrder';
|
|
83
|
+
}
|
|
84
|
+
return event;
|
|
85
|
+
};
|
|
86
|
+
const prepareData = (data, event) => {
|
|
87
|
+
if (event === 'Search') {
|
|
88
|
+
return data && data['search'] ? { query: data['search'] } : {};
|
|
89
|
+
}
|
|
90
|
+
if (event !== 'AddToCart' &&
|
|
91
|
+
event !== 'ViewContent' &&
|
|
92
|
+
event !== 'PlaceAnOrder' &&
|
|
93
|
+
event !== 'CompletePayment') {
|
|
94
|
+
return {};
|
|
95
|
+
}
|
|
96
|
+
const payload = {};
|
|
97
|
+
if (data['value']) {
|
|
98
|
+
payload['value'] = data['value'];
|
|
99
|
+
}
|
|
100
|
+
if (data['currency']) {
|
|
101
|
+
payload['currency'] = data['currency'];
|
|
102
|
+
}
|
|
103
|
+
if (data['status']) {
|
|
104
|
+
payload['status'] = data['status'];
|
|
105
|
+
}
|
|
106
|
+
if (data['description']) {
|
|
107
|
+
payload['description'] = data['description'];
|
|
108
|
+
}
|
|
109
|
+
if (data['contents'] &&
|
|
110
|
+
Array.isArray(data['contents']) &&
|
|
111
|
+
data['contents'].length > 1) {
|
|
112
|
+
payload['contents'] = data['contents'].map((item) => {
|
|
113
|
+
const content = {};
|
|
114
|
+
if (item.id) {
|
|
115
|
+
content['content_id'] = item.id;
|
|
116
|
+
}
|
|
117
|
+
if (item.quantity) {
|
|
118
|
+
content['quantity'] = item.quantity;
|
|
119
|
+
}
|
|
120
|
+
if (item.item_price) {
|
|
121
|
+
content['price'] = item.item_price;
|
|
122
|
+
}
|
|
123
|
+
if (item.category) {
|
|
124
|
+
content['content_category'] = item.category;
|
|
125
|
+
}
|
|
126
|
+
if (item.title) {
|
|
127
|
+
content['content_name'] = item.title;
|
|
128
|
+
}
|
|
129
|
+
if (item.type) {
|
|
130
|
+
content['content_type'] = item.type;
|
|
131
|
+
}
|
|
132
|
+
return content;
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
if (data['contents'] &&
|
|
136
|
+
Array.isArray(data['contents']) &&
|
|
137
|
+
data['contents'].length === 1) {
|
|
138
|
+
if (data['contents'][0].item_price) {
|
|
139
|
+
payload['price'] = data['contents'][0].item_price;
|
|
140
|
+
}
|
|
141
|
+
if (data['contents'][0].quantity) {
|
|
142
|
+
payload['quantity'] = data['contents'][0].quantity;
|
|
143
|
+
}
|
|
144
|
+
if (data['contents'][0].id) {
|
|
145
|
+
payload['content_id'] = data['contents'][0].id;
|
|
146
|
+
}
|
|
147
|
+
if (data['contents'][0].title) {
|
|
148
|
+
payload['content_name'] = data['contents'][0].title;
|
|
149
|
+
}
|
|
150
|
+
if (data['contents'][0].type) {
|
|
151
|
+
payload['content_type'] = data['contents'][0].type;
|
|
152
|
+
}
|
|
153
|
+
if (data['contents'][0].category) {
|
|
154
|
+
payload['content_category'] = data['contents'][0].category;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
payload['app'] = 'Blotout';
|
|
158
|
+
return payload;
|
|
159
|
+
};
|
|
160
|
+
const tag = ({ data, eventName, manifestVariables, eventId }) => {
|
|
161
|
+
if (!window.ttq || manifestVariables['enableBrowser'] !== '1') {
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const tiktokEventName = getEventName(eventName);
|
|
165
|
+
window.ttq.instance(manifestVariables['pixelId']).track(tiktokEventName, Object.assign(Object.assign({}, prepareData(data, tiktokEventName)), { event_id: eventId }));
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
/* eslint-enable */
|
|
169
|
+
const data = {
|
|
170
|
+
name: 'tiktok',
|
|
171
|
+
tag,
|
|
172
|
+
init,
|
|
173
|
+
};
|
|
174
|
+
try {
|
|
175
|
+
if (window) {
|
|
176
|
+
if (!window.edgetagProviders) {
|
|
177
|
+
window.edgetagProviders = [];
|
|
178
|
+
}
|
|
179
|
+
window.edgetagProviders.push(data);
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
catch (_a) {
|
|
183
|
+
// No window
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
return data;
|
|
187
|
+
|
|
188
|
+
})();
|
package/package.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@blotoutio/providers-tiktok-sdk",
|
|
3
|
+
"version": "0.8.0",
|
|
4
|
+
"description": "Tiktok Browser SDK for EdgeTag",
|
|
5
|
+
"author": "Blotout",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://github.com/blotoutio/edgetag-sdk",
|
|
8
|
+
"publishConfig": {
|
|
9
|
+
"access": "public"
|
|
10
|
+
},
|
|
11
|
+
"main": "./index.js",
|
|
12
|
+
"repository": {
|
|
13
|
+
"type": "git",
|
|
14
|
+
"url": "git+https://github.com/blotoutio/edgetag-sdk.git"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"index.js",
|
|
18
|
+
"package.json",
|
|
19
|
+
"README.md"
|
|
20
|
+
]
|
|
21
|
+
}
|