forked from sajal/async-DFP-ads
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dfp-ads.js
98 lines (80 loc) · 2.48 KB
/
dfp-ads.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
87
88
89
90
91
92
93
94
95
96
97
98
(function() {
if (location.protocol == 'https:')
return;
var myads = [],
_docwrite = document.write;
$LAB.script("http://partner.googleadservices.com/gampad/google_service.js").wait(function() {
GS_googleAddAdSenseService(DFPpubid);
GS_googleEnableAllServices();
});
document.write = function(str) {
// GA_googleFillSlotWithSize calls call document.write which should
// trigger this case
if (checkForDFPAd(str))
return;
// GS_googleEnableAllServices calls document.write to load more scripts
// the original LABjs load at the top should trigger this case
if (checkForDFPLoader(str))
return;
// otherwise, just use regular document.write. See this documentation:
// http://paulbakaus.com/2009/02/12/defer-documentwrite/
if (navigator.userAgent.indexOf('MSIE') > -1) {
_docwrite(str)
} else {
_docwrite.call(document, str);
}
}
function displayAd(slotname) {
var container = document.getElementById('dfp-ad-'+slotname);
GA_googleFillSlotWithSize(DFPpubid, slotname, container.offsetWidth, container.offsetHeight);
}
// all of google's libraries for loading ads have come in, so we can now
// specify which ads we want on the page
function dfpReady() {
for (var i = 0, m = DFPads.length; i < m; i++) {
myads.push(DFPads[i]);
displayAd(DFPads[i]);
}
// now that we're ready with DFP's scripts, DFPads.push()
// should immediately call displayAd.
DFPads = {
push : function(slotname) {
myads.push(slotname);
displayAd(slotname);
}
};
}
function checkForDFPAd(str) {
var escaped_slotnames = [],
i = myads.length,
re;
while(i--) {
escaped_slotnames.push(myads[i].replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"));
}
re = new RegExp('google_ads_div_('+escaped_slotnames.join('|')+')');
if (match = re.exec(str)) {
document.getElementById('dfp-ad-'+match[1]).innerHTML = str;
return true;
}
}
function checkForDFPLoader(str) {
var re = /script src=['"](https?:\/\/partner.googleadservices.+?)['"]/ig,
urls = [],
match;
// global match of script urls (like preg_match_all)
while (match = re.exec(str))
urls.push(match[1]);
if (urls.length) {
// load the document.write scripts in order
// check at the end if we have iframerendering available
$LAB.script(urls).wait(function() {
if (typeof GA_googleUseIframeRendering !== 'undefined') {
GA_googleUseIframeRendering();
dfpReady();
}
});
return true;
}
return false;
}
}());