-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps-feeder.ts
392 lines (269 loc) · 11.2 KB
/
ps-feeder.ts
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// @ts-ignore
import FeedData from 'FeedData' assert { type: 'json' }
interface Product {
id: number;
sku?: string;
title: string;
url: string;
image: string;
price: number;
}
type GWDEventType = 'pageload' | 'action'
interface GWD {
actions?: {
gwdGenericad: {
goToPage: (type: string, page: string, b: string, w: number, z: string, p: string) => void
},
events: {
addHandler: (id: string, type: GWDEventType, callback: (e: CustomEvent<any>) => void, u: boolean) => void
}
}
}
interface PSFeedContainers {
title: string;
image: string;
price: string;
cta: string;
thumbnail: string;
open: string;
close: string;
}
class PSFeeder {
private $tracked: Array<string> = [];
private $mainPageId: string = '';
private $gwd: GWD = {};
private $products: Product[] = [];
private $containers: PSFeedContainers = { title: '', image: '', price: '', cta: '', thumbnail: '', open: '', close: '' }
constructor(mainPageId: string) {
this.$mainPageId = mainPageId
this.$gwd = (<any>window).gwd
}
static get isGWDAvailable(): boolean {
return (<any>window).gwd && typeof (<any>window).gwd?.actions?.events?.addHandler !== 'undefined'
}
static get isAirtoryAvailable(): boolean {
return (<any>window).airtory && typeof (<any>window).airtory?.track !== 'undefined'
}
static get isSimpliAvailable(): boolean {
return (<any>window).__simpli && typeof (<any>window).__simpli.analytics !== 'undefined'
}
static prefixes(): PSFeedContainers {
return { title: '', image: '', price: '', cta: '', thumbnail: '', open: '', close: '' }
}
static validate(): void {
if (!this.isGWDAvailable) {
throw new TypeError('PS-FEEDER: Google Web Designer must be initialized before use this script.')
}
if (!this.isAirtoryAvailable) {
throw new TypeError('PS-FEEDER: The Airtory library must be loaded before the feeder script.')
}
if (!this.isSimpliAvailable) {
throw new TypeError('PS-FEEDER: The Airtory library must be loaded before the feeder script.')
}
}
sync(): Product[] {
this.$products = (FeedData as Product[]).map((product, index) => ({ ...product, id: index + 1 }) as Product)
return this.products
}
static make(
mainPageId: string,
builder?: (feeder: PSFeeder) => void,
prefix?: PSFeedContainers
): PSFeeder {
PSFeeder.validate()
const feeder = new PSFeeder(mainPageId)
if (typeof builder === 'undefined') {
builder = (feeder) => {
feeder.products.forEach((product) => {
feeder.feed(`${feeder.$mainPageId}_${product.id}`, product.id)
})
}
}
if (!prefix) {
prefix = PSFeeder.prefixes()
}
prefix.cta = prefix.cta || 'exit_url_product_'
prefix.image = prefix.image || 'image_product_'
prefix.price = prefix.price || 'price_product_'
prefix.title = prefix.title || 'title_product_'
prefix.open = prefix.open || 'open_product_'
prefix.close = prefix.close || 'close_product_'
prefix.thumbnail = prefix.thumbnail || 'thumbnail_product_'
feeder
.useContainerPrefixForCTA(prefix.cta)
.useContainerPrefixForImage(prefix.image)
.useContainerPrefixForPrice(prefix.price)
.useContainerPrefixForTitle(prefix.title)
.useContainerPrefixForThumbnail(prefix.thumbnail)
.useContainerPrefixForOpen(prefix.open)
.useContainerPrefixForClose(prefix.close)
.initialize(builder)
return feeder
}
static trackOnAirtory(label: string): void {
(<any>window).airtory.track(label);
}
static trackInteraction(label: string, asset: string): void {
PSFeeder.trackOnAirtory(label);
(<any>window).__simpli
.analytics()
.trackCustomEvent(label, asset, false);
}
static trackInteractionClick(label: string, asset: string, url: string): void {
PSFeeder.trackOnAirtory(label);
(<any>window).__simpli
.analytics()
.trackClickEvent(label, asset, url)
}
static preload(products: Product[]): void {
products.forEach((product) => {
const link = document.createElement('link')
link.setAttribute('rel', 'preload')
link.setAttribute('type', `image/${product.image.substring(product.image.lastIndexOf('.') + 1).toLocaleLowerCase()}`)
link.setAttribute('crossorigin', "true")
link.setAttribute('href', product.image)
link.setAttribute('as', 'image')
document.head.appendChild(link)
})
}
initialize(callback: (feeder: PSFeeder) => void): PSFeeder {
this.sync()
this.onGWDPageLoad(this.$mainPageId, (e: CustomEvent<any>) => this.feedMainPage(e))
this.products.forEach((product) => {
this.feed(`${this.$mainPageId}_${product.id}`, product.id)
})
callback(this)
return this
}
get products(): Product[] {
return this.$products
}
useContainerPrefixForPrice(prefix: string): PSFeeder {
this.$containers.price = prefix
return this
}
useContainerPrefixForTitle(prefix: string): PSFeeder {
this.$containers.title = prefix
return this
}
useContainerPrefixForImage(prefix: string): PSFeeder {
this.$containers.image = prefix
return this
}
useContainerPrefixForCTA(prefix: string): PSFeeder {
this.$containers.cta = prefix
return this
}
useContainerPrefixForThumbnail(prefix: string): PSFeeder {
this.$containers.thumbnail = prefix
return this
}
useContainerPrefixForOpen(prefix: string): PSFeeder {
this.$containers.open = prefix
return this
}
useContainerPrefixForClose(prefix: string): PSFeeder {
this.$containers.close = prefix
return this
}
feedProductImage(gwdPage: HTMLElement, product: Product, containerPrefix: string): PSFeeder {
const image = gwdPage.querySelector(`#${containerPrefix}${product.id} img`) as HTMLImageElement;
image.src = product.image
image.style.backgroundImage = `url(${product.image})`
return this
}
feedProductTitle(gwdPage: HTMLElement, product: Product, containerPrefix: string): PSFeeder {
const element = gwdPage.querySelector(`#${containerPrefix}${product.id}`) as HTMLParagraphElement
element.textContent = product.title
return this
}
feedProductPrice(gwdPage: HTMLElement, product: Product, containerPrefix: string): PSFeeder {
const element = gwdPage.querySelector(`#${containerPrefix}${product.id}`) as HTMLParagraphElement
element.textContent = product.price.toString()
return this
}
feedProductTrackers(page: HTMLElement, product: Product): PSFeeder {
return this
.feedProductTrackerOpen(page, product)
.feedProductTrackerClose(page, product)
.feedProductTrackerCTA(page, product)
}
feedProductTrackerClose(element: HTMLElement, product: Product): PSFeeder {
const label = `${this.$containers.close}${product.id}`;
if (this.$tracked.indexOf(label) > -1) {
return this
}
this.$tracked.push(label)
return this.onGWDAction(label, () => {
PSFeeder.trackInteraction(`user_tap_close_product_${product.id}`, element.id)
PSFeeder.trackOnAirtory(`auto_tap_close_product_${product.sku}`)
// Close the page and return to the main page
this.$gwd?.actions?.gwdGenericad.goToPage('gwd-ad', this.$mainPageId, 'none', 300, 'linear', 'top');
})
}
feedProductTrackerOpen(element: HTMLElement, product: Product): PSFeeder {
const label = `${this.$containers.open}${product.id}`;
if (this.$tracked.indexOf(label) > -1) {
return this
}
this.$tracked.push(label)
return this.onGWDAction(label, () => {
PSFeeder.trackInteraction(`user_tap_open_product_${product.id}`, element.id)
PSFeeder.trackOnAirtory(`auto_tap_open_product_${product.sku}`)
// Open the page selected
this.$gwd?.actions?.gwdGenericad.goToPage('gwd-ad', `${this.$mainPageId}_${product.id}`, 'none', 300, 'linear', 'top');
})
}
feedProductTrackerCTA(element: HTMLElement, product: Product): PSFeeder {
const label = `${this.$containers.cta}${product.id}`;
if (this.$tracked.indexOf(label) > -1) {
return this
}
this.$tracked.push(label)
return this.onGWDAction(label, () => {
PSFeeder.trackInteractionClick(`user_tap_exitUrl_product_${product.id}`, element.id, product.url)
PSFeeder.trackOnAirtory(`auto_tap_exitUrl_product_${product.sku}`)
})
}
feedMainPage({ target }: CustomEvent<{ target: HTMLElement }>): void {
this.products.forEach((product) => {
this
.feedProductImage(target as HTMLElement, product, this.$containers.thumbnail)
.feedProductTrackers(target as HTMLElement, product)
})
}
feed(pageId: string, withProductNumber: number): void {
this.onGWDPageLoad(pageId, () => {
let index = withProductNumber - 1;
const page = document.getElementById(pageId) as HTMLElement
if (!page) {
throw new TypeError('PS-FEEDER: Invalid GWD page ID.')
}
if (!withProductNumber) {
throw new TypeError('PS-FEEDER: The product offset is required to feed.')
}
if (index < 0 || index > this.products.length || typeof this.products[index] === 'undefined') {
throw new TypeError('PS-FEEDER: Invalid product offset or not exists.')
}
const product = this.products[index]
this
.feedProductImage(page, product, this.$containers.image)
.feedProductTitle(page, product, this.$containers.title)
.feedProductPrice(page, product, this.$containers.price)
.feedProductTrackers(page, product)
})
}
onGWDPageLoad(pageId: string, callback: (e: CustomEvent<any>) => void): PSFeeder {
return this.on(pageId, 'pageload', callback)
}
onGWDAction(targetId: string, callback: (e?: CustomEvent<any>) => void): PSFeeder {
return this.on(targetId, 'action', callback)
}
on(gwdElementId: string, gwdEventType: GWDEventType = 'action', callback: (e: CustomEvent<any>) => void): PSFeeder {
this.$gwd?.actions?.events.addHandler(gwdElementId, gwdEventType, callback, false)
document.addEventListener("unload", () => callback({} as CustomEvent));
return this
}
}
const script: HTMLScriptElement = document.getElementById('ps-feeder') as HTMLScriptElement;
PSFeeder.make(script.dataset?.mainPage?.toString() || 'main_page')