-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
86 lines (62 loc) · 2.2 KB
/
index.js
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
'use strict';
import Ad from './src/Ad'
export default {
// Default sizes
defaultSizes : {
banner: [[1200, 600], [1200, 400], [300, 250], [300, 300]],
rectangle: [ [300, 250], [300, 300], [300, 600]]
},
install(Vue, options){
Vue.component('google-ad', Ad);
this.init(options, Vue);
},
init(options, Vue){
// Following Admanagers tag generation - June 6, 2019
// Create Google tag
window.googletag = {};
googletag.cmd = googletag.cmd || [];
// Create script reference to google ad manager
let googleScript = document.createElement('script');
googleScript.async = true;
googleScript.type = 'text/javascript';
googleScript.src = 'https://www.googletagservices.com/tag/js/gpt.js'
// Reference head of document
var head = document.getElementsByTagName('head')[0];
head.appendChild(googleScript)
// Define google tag slots
googletag.cmd.push(() => {
// Loop through and define slots
this.defineOptions(options, Vue);
googletag.pubads().enableSingleRequest();
googletag.pubads().collapseEmptyDivs();
googletag.enableServices();
});
},
defineOptions(options, Vue){
// If user doesn't pass in ad default size, define default
if(!options.default_size){
options.default_size = "rectangle";
}
// If user doesn't pass in sizes object, set to default defined above
if(!options.sizes){
options.sizes = this.defaultSizes
}
// Required. User must pass in mappings object
options.mappings = this.createMappings(options.mappings);
// Set googlead options to use in Ad
Vue.prototype.$googlead = options;
},
createMappings(mappings){
Object.keys(mappings).forEach((key) => {
mappings[key] = this.setSize(mappings[key])
});
return mappings;
},
setSize(mapping){
let mapper = googletag.sizeMapping();
mapping.forEach((map) => {
mapper.addSize(map.window, map.sizes)
});
return mapper.build();
},
}