-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_with_slot_mapping_in_head.patch
161 lines (154 loc) · 6.06 KB
/
async_with_slot_mapping_in_head.patch
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
diff --git a/dfp.module b/dfp.module
index b9be29c..3396209 100644
--- a/dfp.module
+++ b/dfp.module
@@ -754,6 +754,8 @@ function _dfp_js_global_settings() {
$js .= 'googletag.cmd.push(function() {' . "\n";
if (variable_get('dfp_async_rendering', 1)) {
+ $js .= 'var mapping;'. "\n";
+ $js .= implode(_dfp_js_slot_definition());
$js .= ' googletag.pubads().enableAsyncRendering();' . "\n";
}
else {
@@ -800,14 +802,15 @@ function _dfp_js_global_settings() {
if (variable_get('dfp_async_rendering', 1)) {
$js .= '});' . "\n";
- $options = array(
- 'type' => 'inline',
- 'group' => JS_DEFAULT,
- 'every_page' => TRUE,
- 'weight' => 10,
- 'force header' => TRUE,
+ $element = array(
+ '#tag' => 'script',
+ '#weight' => 100000,
+ '#value' => str_replace(array("\n", " "), "", $js),
+ '#attributes' => array(
+ 'type' => 'text/javascript'
+ ),
);
- drupal_add_js($js, $options);
+ drupal_add_html_head($element, 'dfp_googletag_test');
} else {
$element = array(
'#tag' => 'script',
@@ -826,73 +829,65 @@ function _dfp_js_global_settings() {
* Helper function to build the javascript needed to define an ad slot and add
* it to the head tag.
*/
-function _dfp_js_slot_definition($tag) {
+function _dfp_js_slot_definition($tag = NULL) {
+ $tagArray = &drupal_static(__FUNCTION__, array());
// Add the js needed to define this adSlot to <head>.
- $js = '';
-
- if (variable_get('dfp_async_rendering', 1)) {
- $js = 'googletag.cmd.push(function() {';
- }
- // Start by defining breakpoints for this ad.
- if (!empty($tag->breakpoints)) {
- $breakpoints = $tag->breakpoints;
- $js .= 'var mapping = googletag.sizeMapping()' . "\n";
- foreach ($breakpoints as $breakpoint) {
- $js .= ' .addSize(' . dfp_format_size($breakpoint['browser_size']) . ', ' . dfp_format_size($breakpoint['ad_sizes']) . ')' . "\n";
+ if (!empty($tag)) {
+ $js = '';
+
+ // Start by defining breakpoints for this ad.
+ if (!empty($tag->breakpoints)) {
+ $breakpoints = $tag->breakpoints;
+ $js .= 'mapping = googletag.sizeMapping()' . "\n";
+ foreach ($breakpoints as $breakpoint) {
+ $js .= ' .addSize(' . dfp_format_size($breakpoint['browser_size']) . ', ' . dfp_format_size($breakpoint['ad_sizes']) . ')' . "\n";
+ }
+ $js .= ' .build();' . "\n";
}
- $js .= ' .build();' . "\n";
- }
- if (!empty($tag->settings['out_of_page'])) {
- $js .= 'googletag.slots["' . $tag->machinename . '"] = googletag.defineOutOfPageSlot("' . $tag->adunit . '", "' . $tag->placeholder_id . '")' . "\n";
- }
- else {
- $js .= 'googletag.slots["' . $tag->machinename . '"] = googletag.defineSlot("' . $tag->adunit . '", ' . $tag->size . ', "' . $tag->placeholder_id . '")' . "\n";
- }
- $click_url = variable_get('dfp_click_url', '');
- if (!empty($click_url)) {
- if (!preg_match("/^https?:\/\//", $click_url)) {
- // relative URL; prepend site domain
- $click_url = (($GLOBALS['is_https']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . "/" . $click_url;
+ if (!empty($tag->settings['out_of_page'])) {
+ $js .= 'googletag.slots["' . $tag->machinename . '"] = googletag.defineOutOfPageSlot("' . $tag->adunit . '", "' . $tag->placeholder_id . '")' . "\n";
}
- $js .= ' .setClickUrl("' . $click_url . '")' . "\n";
- }
- $js .= ' .addService(googletag.pubads())' . "\n";
+ else {
+ $js .= 'googletag.slots["' . $tag->machinename . '"] = googletag.defineSlot("' . $tag->adunit . '", ' . $tag->size . ', "' . $tag->placeholder_id . '")' . "\n";
+ }
+ $click_url = variable_get('dfp_click_url', '');
+ if (!empty($click_url)) {
+ if (!preg_match("/^https?:\/\//", $click_url)) {
+ // relative URL; prepend site domain
+ $click_url = (($GLOBALS['is_https']) ? 'https://' : 'http://') . $_SERVER['HTTP_HOST'] . "/" . $click_url;
+ }
+ $js .= ' .setClickUrl("' . $click_url . '")' . "\n";
+ }
+ $js .= ' .addService(googletag.pubads())' . "\n";
- if (!empty($tag->adsense_ad_types)) {
- $js .= ' .set("adsense_ad_types", "' . $tag->adsense_ad_types . '")' . "\n";
- }
- if (!empty($tag->adsense_channel_ids)) {
- $js .= ' .set("adsense_channel_ids", "' . $tag->adsense_channel_ids . '")' . "\n";
- }
- foreach ($tag->adsense_colors as $key => $val) {
- if (!empty($val)) {
- $key = 'adsense_' . $key . '_color';
- $val = '#' . drupal_strtoupper($val);
- $js .= ' .set("' . $key . '", "' . $val . '")' . "\n";
+ if (!empty($tag->adsense_ad_types)) {
+ $js .= ' .set("adsense_ad_types", "' . $tag->adsense_ad_types . '")' . "\n";
+ }
+ if (!empty($tag->adsense_channel_ids)) {
+ $js .= ' .set("adsense_channel_ids", "' . $tag->adsense_channel_ids . '")' . "\n";
+ }
+ foreach ($tag->adsense_colors as $key => $val) {
+ if (!empty($val)) {
+ $key = 'adsense_' . $key . '_color';
+ $val = '#' . drupal_strtoupper($val);
+ $js .= ' .set("' . $key . '", "' . $val . '")' . "\n";
+ }
+ }
+ $targeting = dfp_format_targeting($tag->targeting, $tag);
+ foreach ($targeting as $target) {
+ $js .= ' .setTargeting(' . $target['target'] . ', ' . $target['value'] . ')' . "\n";
+ }
+ // Apply size mapping when there are breakpoints.
+ if (!empty($tag->breakpoints)) {
+ $js .= ' .defineSizeMapping(mapping)' . "\n";
}
- }
- $targeting = dfp_format_targeting($tag->targeting, $tag);
- foreach ($targeting as $target) {
- $js .= ' .setTargeting(' . $target['target'] . ', ' . $target['value'] . ')' . "\n";
- }
- // Apply size mapping when there are breakpoints.
- if (!empty($tag->breakpoints)) {
- $js .= ' .defineSizeMapping(mapping)' . "\n";
- }
- // Closing googletag.cmd.push
- if (variable_get('dfp_async_rendering', 1)) {
- $js .= ' })';
- }
- $js = rtrim($js, "\n") . ';';
+ $js = rtrim($js, "\n") . ';'."\n";
- $options = array(
- 'type' => 'inline',
- 'group' => JS_LIBRARY,
- 'weight' => 0,
- );
+ $tagArray[] = $js;
+ }
- drupal_add_js($js, $options);
+ return $tagArray;
}
/**