@blotoutio/providers-criteo-sdk 0.38.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 ADDED
@@ -0,0 +1,11 @@
1
+ # providers-criteo-sdk
2
+
3
+ This library was generated with [Nx](https://nx.dev).
4
+
5
+ ## Building
6
+
7
+ Run `nx build providers-criteo-sdk` to build the library.
8
+
9
+ ## Running unit tests
10
+
11
+ Run `nx test providers-criteo-sdk` to execute the unit tests via [Jest](https://jestjs.io).
package/index.cjs.js ADDED
@@ -0,0 +1,166 @@
1
+ 'use strict';
2
+
3
+ const packageName = 'criteo';
4
+
5
+ const initCriteo = (ID) => {
6
+ if (!window || !document || window.criteo_q) {
7
+ return;
8
+ }
9
+ window.criteo_q = [];
10
+ const element = document.createElement('script');
11
+ element.async = !0;
12
+ element.src = `https://dynamic.criteo.com/js/ld/ld.js?a=${ID}`;
13
+ const script = document.getElementsByTagName('script')[0];
14
+ if (script && script.parentNode) {
15
+ script.parentNode.insertBefore(element, script);
16
+ }
17
+ };
18
+ const init = ({ userId, manifest }) => {
19
+ if (!window ||
20
+ !manifest.variables ||
21
+ manifest.variables['enableBrowser'] !== '1' ||
22
+ !manifest.variables['partnerId']) {
23
+ return;
24
+ }
25
+ initCriteo(manifest.variables['partnerId']);
26
+ const deviceType = /iPad/.test(navigator.userAgent)
27
+ ? 't'
28
+ : /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk/.test(navigator.userAgent)
29
+ ? 'm'
30
+ : 'd';
31
+ if (window && window.criteo_q) {
32
+ window.criteo_q.push({ event: 'setAccount', account: manifest.variables['partnerId'] }, { event: 'setSiteType', type: deviceType }, { event: 'setRetailerVisitorId', id: userId });
33
+ }
34
+ };
35
+
36
+ const getData = (eventName, data) => {
37
+ var _a, _b, _c;
38
+ const eventData = {
39
+ event: getCriteoEventName(eventName),
40
+ };
41
+ if (eventName === 'ViewContent') {
42
+ if ((_a = data === null || data === void 0 ? void 0 : data['contents']) === null || _a === void 0 ? void 0 : _a.length) {
43
+ const content = (data === null || data === void 0 ? void 0 : data['contents'])[0];
44
+ eventData['item'] = content.id;
45
+ eventData['price'] = content.item_price;
46
+ }
47
+ }
48
+ else {
49
+ if (data === null || data === void 0 ? void 0 : data['currency']) {
50
+ eventData['currency'] = data['currency'];
51
+ }
52
+ if ((_b = data === null || data === void 0 ? void 0 : data['contents']) === null || _b === void 0 ? void 0 : _b.length) {
53
+ eventData['item'] = (_c = data === null || data === void 0 ? void 0 : data['contents']) === null || _c === void 0 ? void 0 : _c.map((content) => ({
54
+ id: content.id,
55
+ price: content.item_price,
56
+ quantity: content.quantity,
57
+ }));
58
+ }
59
+ }
60
+ if (eventName === 'Purchase') {
61
+ if (data === null || data === void 0 ? void 0 : data['orderId']) {
62
+ eventData['id'] = data['orderId'];
63
+ }
64
+ }
65
+ return eventData;
66
+ };
67
+ const getCriteoEventName = (name) => {
68
+ switch (name) {
69
+ case 'PageView': {
70
+ return 'viewPage';
71
+ }
72
+ case 'ViewContent': {
73
+ return 'viewItem';
74
+ }
75
+ case 'AddToCart': {
76
+ return 'addToCart';
77
+ }
78
+ case 'InitiateCheckout': {
79
+ return 'viewBasket';
80
+ }
81
+ case 'Purchase': {
82
+ return 'trackTransaction';
83
+ }
84
+ default: {
85
+ return '';
86
+ }
87
+ }
88
+ };
89
+ const handleTag = ({ data, eventName, manifestVariables, }) => {
90
+ var _a;
91
+ if (!window || !eventName || !(manifestVariables === null || manifestVariables === void 0 ? void 0 : manifestVariables['partnerId'])) {
92
+ return;
93
+ }
94
+ const eventData = getData(eventName, data);
95
+ if (eventData === null || eventData === void 0 ? void 0 : eventData.event) {
96
+ (_a = window.criteo_q) === null || _a === void 0 ? void 0 : _a.push(eventData);
97
+ }
98
+ };
99
+
100
+ const tag = ({ data, eventName, manifestVariables }) => {
101
+ const payload = {
102
+ sdkVersion: "0.38.0" ,
103
+ };
104
+ if (window.criteo_q && manifestVariables['enableBrowser'] === '1') {
105
+ handleTag({ data, eventName, manifestVariables });
106
+ }
107
+ return payload;
108
+ };
109
+
110
+ const sha = async (algorithm, text) => {
111
+ if (!text) {
112
+ return '';
113
+ }
114
+ const encodedText = new TextEncoder().encode(text);
115
+ const hashBuffer = await crypto.subtle.digest({
116
+ name: algorithm,
117
+ }, encodedText);
118
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
119
+ return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
120
+ };
121
+ const sha256 = async (text) => {
122
+ return await sha('SHA-256', text);
123
+ };
124
+
125
+ const user = ({ data }) => {
126
+ var _a;
127
+ if (!window || !window.criteo_q || !data) {
128
+ return;
129
+ }
130
+ if (data['zip']) {
131
+ (_a = window.criteo_q) === null || _a === void 0 ? void 0 : _a.push({ event: 'setZipcode', zipcode: data['zip'] });
132
+ }
133
+ if (data['email']) {
134
+ sha256(data['email'].toString())
135
+ .then((value) => {
136
+ var _a;
137
+ (_a = window.criteo_q) === null || _a === void 0 ? void 0 : _a.push({
138
+ event: 'setEmail',
139
+ email: value,
140
+ hash_method: 'sha256',
141
+ });
142
+ })
143
+ .catch(() => undefined);
144
+ }
145
+ };
146
+
147
+ // eslint-disable-next-line @nx/enforce-module-boundaries
148
+ const data = {
149
+ name: packageName,
150
+ init,
151
+ tag,
152
+ user,
153
+ };
154
+ try {
155
+ if (window) {
156
+ if (!window.edgetagProviders) {
157
+ window.edgetagProviders = [];
158
+ }
159
+ window.edgetagProviders.push(data);
160
+ }
161
+ }
162
+ catch {
163
+ // No window
164
+ }
165
+
166
+ module.exports = data;
package/index.js ADDED
@@ -0,0 +1,169 @@
1
+ var ProvidersCriteoSdk = (function () {
2
+ 'use strict';
3
+
4
+ const packageName = 'criteo';
5
+
6
+ const initCriteo = (ID) => {
7
+ if (!window || !document || window.criteo_q) {
8
+ return;
9
+ }
10
+ window.criteo_q = [];
11
+ const element = document.createElement('script');
12
+ element.async = !0;
13
+ element.src = `https://dynamic.criteo.com/js/ld/ld.js?a=${ID}`;
14
+ const script = document.getElementsByTagName('script')[0];
15
+ if (script && script.parentNode) {
16
+ script.parentNode.insertBefore(element, script);
17
+ }
18
+ };
19
+ const init = ({ userId, manifest }) => {
20
+ if (!window ||
21
+ !manifest.variables ||
22
+ manifest.variables['enableBrowser'] !== '1' ||
23
+ !manifest.variables['partnerId']) {
24
+ return;
25
+ }
26
+ initCriteo(manifest.variables['partnerId']);
27
+ const deviceType = /iPad/.test(navigator.userAgent)
28
+ ? 't'
29
+ : /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk/.test(navigator.userAgent)
30
+ ? 'm'
31
+ : 'd';
32
+ if (window && window.criteo_q) {
33
+ window.criteo_q.push({ event: 'setAccount', account: manifest.variables['partnerId'] }, { event: 'setSiteType', type: deviceType }, { event: 'setRetailerVisitorId', id: userId });
34
+ }
35
+ };
36
+
37
+ const getData = (eventName, data) => {
38
+ var _a, _b, _c;
39
+ const eventData = {
40
+ event: getCriteoEventName(eventName),
41
+ };
42
+ if (eventName === 'ViewContent') {
43
+ if ((_a = data === null || data === void 0 ? void 0 : data['contents']) === null || _a === void 0 ? void 0 : _a.length) {
44
+ const content = (data === null || data === void 0 ? void 0 : data['contents'])[0];
45
+ eventData['item'] = content.id;
46
+ eventData['price'] = content.item_price;
47
+ }
48
+ }
49
+ else {
50
+ if (data === null || data === void 0 ? void 0 : data['currency']) {
51
+ eventData['currency'] = data['currency'];
52
+ }
53
+ if ((_b = data === null || data === void 0 ? void 0 : data['contents']) === null || _b === void 0 ? void 0 : _b.length) {
54
+ eventData['item'] = (_c = data === null || data === void 0 ? void 0 : data['contents']) === null || _c === void 0 ? void 0 : _c.map((content) => ({
55
+ id: content.id,
56
+ price: content.item_price,
57
+ quantity: content.quantity,
58
+ }));
59
+ }
60
+ }
61
+ if (eventName === 'Purchase') {
62
+ if (data === null || data === void 0 ? void 0 : data['orderId']) {
63
+ eventData['id'] = data['orderId'];
64
+ }
65
+ }
66
+ return eventData;
67
+ };
68
+ const getCriteoEventName = (name) => {
69
+ switch (name) {
70
+ case 'PageView': {
71
+ return 'viewPage';
72
+ }
73
+ case 'ViewContent': {
74
+ return 'viewItem';
75
+ }
76
+ case 'AddToCart': {
77
+ return 'addToCart';
78
+ }
79
+ case 'InitiateCheckout': {
80
+ return 'viewBasket';
81
+ }
82
+ case 'Purchase': {
83
+ return 'trackTransaction';
84
+ }
85
+ default: {
86
+ return '';
87
+ }
88
+ }
89
+ };
90
+ const handleTag = ({ data, eventName, manifestVariables, }) => {
91
+ var _a;
92
+ if (!window || !eventName || !(manifestVariables === null || manifestVariables === void 0 ? void 0 : manifestVariables['partnerId'])) {
93
+ return;
94
+ }
95
+ const eventData = getData(eventName, data);
96
+ if (eventData === null || eventData === void 0 ? void 0 : eventData.event) {
97
+ (_a = window.criteo_q) === null || _a === void 0 ? void 0 : _a.push(eventData);
98
+ }
99
+ };
100
+
101
+ const tag = ({ data, eventName, manifestVariables }) => {
102
+ const payload = {
103
+ sdkVersion: "0.38.0" ,
104
+ };
105
+ if (window.criteo_q && manifestVariables['enableBrowser'] === '1') {
106
+ handleTag({ data, eventName, manifestVariables });
107
+ }
108
+ return payload;
109
+ };
110
+
111
+ const sha = async (algorithm, text) => {
112
+ if (!text) {
113
+ return '';
114
+ }
115
+ const encodedText = new TextEncoder().encode(text);
116
+ const hashBuffer = await crypto.subtle.digest({
117
+ name: algorithm,
118
+ }, encodedText);
119
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
120
+ return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
121
+ };
122
+ const sha256 = async (text) => {
123
+ return await sha('SHA-256', text);
124
+ };
125
+
126
+ const user = ({ data }) => {
127
+ var _a;
128
+ if (!window || !window.criteo_q || !data) {
129
+ return;
130
+ }
131
+ if (data['zip']) {
132
+ (_a = window.criteo_q) === null || _a === void 0 ? void 0 : _a.push({ event: 'setZipcode', zipcode: data['zip'] });
133
+ }
134
+ if (data['email']) {
135
+ sha256(data['email'].toString())
136
+ .then((value) => {
137
+ var _a;
138
+ (_a = window.criteo_q) === null || _a === void 0 ? void 0 : _a.push({
139
+ event: 'setEmail',
140
+ email: value,
141
+ hash_method: 'sha256',
142
+ });
143
+ })
144
+ .catch(() => undefined);
145
+ }
146
+ };
147
+
148
+ // eslint-disable-next-line @nx/enforce-module-boundaries
149
+ const data = {
150
+ name: packageName,
151
+ init,
152
+ tag,
153
+ user,
154
+ };
155
+ try {
156
+ if (window) {
157
+ if (!window.edgetagProviders) {
158
+ window.edgetagProviders = [];
159
+ }
160
+ window.edgetagProviders.push(data);
161
+ }
162
+ }
163
+ catch {
164
+ // No window
165
+ }
166
+
167
+ return data;
168
+
169
+ })();
package/index.mjs ADDED
@@ -0,0 +1,164 @@
1
+ const packageName = 'criteo';
2
+
3
+ const initCriteo = (ID) => {
4
+ if (!window || !document || window.criteo_q) {
5
+ return;
6
+ }
7
+ window.criteo_q = [];
8
+ const element = document.createElement('script');
9
+ element.async = !0;
10
+ element.src = `https://dynamic.criteo.com/js/ld/ld.js?a=${ID}`;
11
+ const script = document.getElementsByTagName('script')[0];
12
+ if (script && script.parentNode) {
13
+ script.parentNode.insertBefore(element, script);
14
+ }
15
+ };
16
+ const init = ({ userId, manifest }) => {
17
+ if (!window ||
18
+ !manifest.variables ||
19
+ manifest.variables['enableBrowser'] !== '1' ||
20
+ !manifest.variables['partnerId']) {
21
+ return;
22
+ }
23
+ initCriteo(manifest.variables['partnerId']);
24
+ const deviceType = /iPad/.test(navigator.userAgent)
25
+ ? 't'
26
+ : /Mobile|iP(hone|od)|Android|BlackBerry|IEMobile|Silk/.test(navigator.userAgent)
27
+ ? 'm'
28
+ : 'd';
29
+ if (window && window.criteo_q) {
30
+ window.criteo_q.push({ event: 'setAccount', account: manifest.variables['partnerId'] }, { event: 'setSiteType', type: deviceType }, { event: 'setRetailerVisitorId', id: userId });
31
+ }
32
+ };
33
+
34
+ const getData = (eventName, data) => {
35
+ var _a, _b, _c;
36
+ const eventData = {
37
+ event: getCriteoEventName(eventName),
38
+ };
39
+ if (eventName === 'ViewContent') {
40
+ if ((_a = data === null || data === void 0 ? void 0 : data['contents']) === null || _a === void 0 ? void 0 : _a.length) {
41
+ const content = (data === null || data === void 0 ? void 0 : data['contents'])[0];
42
+ eventData['item'] = content.id;
43
+ eventData['price'] = content.item_price;
44
+ }
45
+ }
46
+ else {
47
+ if (data === null || data === void 0 ? void 0 : data['currency']) {
48
+ eventData['currency'] = data['currency'];
49
+ }
50
+ if ((_b = data === null || data === void 0 ? void 0 : data['contents']) === null || _b === void 0 ? void 0 : _b.length) {
51
+ eventData['item'] = (_c = data === null || data === void 0 ? void 0 : data['contents']) === null || _c === void 0 ? void 0 : _c.map((content) => ({
52
+ id: content.id,
53
+ price: content.item_price,
54
+ quantity: content.quantity,
55
+ }));
56
+ }
57
+ }
58
+ if (eventName === 'Purchase') {
59
+ if (data === null || data === void 0 ? void 0 : data['orderId']) {
60
+ eventData['id'] = data['orderId'];
61
+ }
62
+ }
63
+ return eventData;
64
+ };
65
+ const getCriteoEventName = (name) => {
66
+ switch (name) {
67
+ case 'PageView': {
68
+ return 'viewPage';
69
+ }
70
+ case 'ViewContent': {
71
+ return 'viewItem';
72
+ }
73
+ case 'AddToCart': {
74
+ return 'addToCart';
75
+ }
76
+ case 'InitiateCheckout': {
77
+ return 'viewBasket';
78
+ }
79
+ case 'Purchase': {
80
+ return 'trackTransaction';
81
+ }
82
+ default: {
83
+ return '';
84
+ }
85
+ }
86
+ };
87
+ const handleTag = ({ data, eventName, manifestVariables, }) => {
88
+ var _a;
89
+ if (!window || !eventName || !(manifestVariables === null || manifestVariables === void 0 ? void 0 : manifestVariables['partnerId'])) {
90
+ return;
91
+ }
92
+ const eventData = getData(eventName, data);
93
+ if (eventData === null || eventData === void 0 ? void 0 : eventData.event) {
94
+ (_a = window.criteo_q) === null || _a === void 0 ? void 0 : _a.push(eventData);
95
+ }
96
+ };
97
+
98
+ const tag = ({ data, eventName, manifestVariables }) => {
99
+ const payload = {
100
+ sdkVersion: "0.38.0" ,
101
+ };
102
+ if (window.criteo_q && manifestVariables['enableBrowser'] === '1') {
103
+ handleTag({ data, eventName, manifestVariables });
104
+ }
105
+ return payload;
106
+ };
107
+
108
+ const sha = async (algorithm, text) => {
109
+ if (!text) {
110
+ return '';
111
+ }
112
+ const encodedText = new TextEncoder().encode(text);
113
+ const hashBuffer = await crypto.subtle.digest({
114
+ name: algorithm,
115
+ }, encodedText);
116
+ const hashArray = Array.from(new Uint8Array(hashBuffer));
117
+ return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
118
+ };
119
+ const sha256 = async (text) => {
120
+ return await sha('SHA-256', text);
121
+ };
122
+
123
+ const user = ({ data }) => {
124
+ var _a;
125
+ if (!window || !window.criteo_q || !data) {
126
+ return;
127
+ }
128
+ if (data['zip']) {
129
+ (_a = window.criteo_q) === null || _a === void 0 ? void 0 : _a.push({ event: 'setZipcode', zipcode: data['zip'] });
130
+ }
131
+ if (data['email']) {
132
+ sha256(data['email'].toString())
133
+ .then((value) => {
134
+ var _a;
135
+ (_a = window.criteo_q) === null || _a === void 0 ? void 0 : _a.push({
136
+ event: 'setEmail',
137
+ email: value,
138
+ hash_method: 'sha256',
139
+ });
140
+ })
141
+ .catch(() => undefined);
142
+ }
143
+ };
144
+
145
+ // eslint-disable-next-line @nx/enforce-module-boundaries
146
+ const data = {
147
+ name: packageName,
148
+ init,
149
+ tag,
150
+ user,
151
+ };
152
+ try {
153
+ if (window) {
154
+ if (!window.edgetagProviders) {
155
+ window.edgetagProviders = [];
156
+ }
157
+ window.edgetagProviders.push(data);
158
+ }
159
+ }
160
+ catch {
161
+ // No window
162
+ }
163
+
164
+ export { data as default };
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@blotoutio/providers-criteo-sdk",
3
+ "version": "0.38.0",
4
+ "description": "Criteo 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.cjs.js",
12
+ "module": "./index.mjs",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/blotoutio/edgetag-sdk.git"
16
+ },
17
+ "files": [
18
+ "index.js",
19
+ "index.cjs.js",
20
+ "index.mjs",
21
+ "package.json",
22
+ "README.md"
23
+ ]
24
+ }