-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreativeManager.js
More file actions
118 lines (99 loc) · 2.71 KB
/
CreativeManager.js
File metadata and controls
118 lines (99 loc) · 2.71 KB
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
import Logger from 'Logger';
import CreativeAnalyzer from 'CreativeAnalyzer';
import PlacementStorage from 'PlacementStorage';
/**
* Creative Manager
* @class CreativeManager
*/
class CreativeManager {
constructor({ publisherId }) {
this.initialze(publisherId);
}
async initialze(publisherId) {
this.publisher = (await this.fetch(`${publisherId}`)).publisher;
// I dont know if publisherId is different from this.publisher.id =)
this.creatives = (await this.fetch(`${this.publisher.id}/creatives/`)).creatives;
}
/**
* asynchronous fetch method
* @param {string} apiUrl
* @returns {object} from response.json()
*/
async fetch(apiUrl) {
const response = await fetch(`/api/publishers/${apiUrl}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
const data = await response.json();
return data;
}
/**
* send logs
* @param {object} creative
*/
sendLogs({ id, name, adType, size, price, placementId}) {
new Logger().send('Creative rendered', {
creative_id: id,
creative_name: name,
creative_type: adType,
creative_size: size,
creative_price: price,
creative_placement: placementId,
});
}
run() {
for (const creative of this.creatives) {
const {
adType,
id,
name,
price,
placementId,
html,
takeoverId,
takeoverParams,
hybrid,
} = creative;
const { verticalHtml, horizontalHtml, vertical, horizontal, takeover } = hybrid;
const notAllowedTakeovers = [2233, 45435, 2352, 6683];
this.sendLogs({id, name, adType, size, price, placementId});
CreativeAnalyzer.run({ id, name, type: adType, price });
if ((adType === 'takeover' || takeover) && notAllowedTakeovers.indexOf(takeoverId) === -1) {
this.renderTakeover(
PlacementStorage.find(placementId, takeoverParams)
);
}
if (adType === 'hybrid') {
// assuming vertical & horizontal are ?.string
const config = {
[vertical]: verticalHtml,
[horizontal]: horizontalHtml,
};
this.render(
PlacementStorage.find(placementId),
config[vertical || horizontal]
);
}
// default simple
this.render(
PlacementStorage.find(placementId),
html
);
}
}
render(placementId, htmlStr) {
const placement = document.getElementById(placementId);
placement.innerHTML = htmlStr;
}
renderTakeover(placementId, params) {
// renders creative in a specific way
}
}
// Usage:
const creativeManager = new CreativeManager({
publisherId: 1,
});
// ....
creativeManager.run();