-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_tag_rendering-1.patch
174 lines (166 loc) · 5 KB
/
async_tag_rendering-1.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
162
163
164
165
166
167
168
169
170
171
172
173
174
diff --git a/dfp.module b/dfp.module
index e1ad37e..57cc668 100755
--- a/dfp.module
+++ b/dfp.module
@@ -660,6 +660,7 @@ function _dfp_get_ad_category($term, $clean_string = FALSE) {
* slot definitions.
*/
function _dfp_js_global_settings() {
+ /*
// Initialize the google varibales and inject user-defined javascript.
$js = 'var googletag = googletag || {};' . "\n";
$js .= 'googletag.cmd = googletag.cmd || [];';
@@ -676,17 +677,80 @@ function _dfp_js_global_settings() {
'force header' => TRUE,
);
drupal_add_js($js, $options);
+ */
+ $googletag = array(
+ 'var googletag = googletag || {};',
+ 'googletag.cmd = googletag.cmd || [];',
+ 'googletag.slots = googletag.slots || {};'
+ );
+
+ if (variable_get('dfp_async_rendering', 1)) {
+ $googleScript = array(
+ '(function() {',
+ 'var gads = document.createElement("script");',
+ 'gads.async = true;',
+ 'gads.type = "text/javascript";',
+ 'var useSSL = "https:" == document.location.protocol;',
+ 'gads.src = (useSSL ? "https:" : "http:") + "//www.googletagservices.com/tag/js/gpt.js";',
+ 'var node = document.getElementsByTagName("script")[0];',
+ 'node.parentNode.insertBefore(gads, node);',
+ '})();'
+ );
+
+ $element = array(
+ '#tag' => 'script',
+ '#weight' => 1000,
+ '#value' => implode($googletag, ' ') . "\n" . implode($googleScript, ' '),
+ '#attributes' => array(
+ 'type' => 'text/javascript',
+ ),
+ );
+
+ drupal_add_html_head($element, 'dfp_googletag_head');
+
+ } else {
+
+ $element = array(
+ '#type' => 'html_tag',
+ '#tag' => 'script',
+ '#value' => '',
+ '#attributes' => array(
+ 'src' => 'http://www.googletagservices.com/tag/js/gpt.js',
+ ),
+ '#suffix' => '</script>'
+ );
+
+ drupal_add_html_head($element, 'dfp_googletag_script_head');
+
+ $element = array(
+ '#tag' => 'script',
+ '#weight' => 1000,
+ '#value' => implode($googletag, ' '),
+ '#attributes' => array(
+ 'type' => 'text/javascript'
+ ),
+ );
+
+ drupal_add_html_head($element, 'dfp_googletag_vars_head');
+ }
// Include a script tag for the Google Tag Services.
// @todo: One day this should include an async=true attribute. See #1140356.
// However, there is a good chance this might break <= IE8.
+ /*
$options['type'] = 'external';
$options['weight']++;
drupal_add_js(($GLOBALS['is_https'] ? "https://" : "http://") . DFP_GOOGLE_TAG_SERVICES_URL, $options);
+ */
// Add global settings with a heavy weight so that they appear after all the
// defineSlot() calls otherwise IE8 and Opera fail to display ads properly.
- $js = 'googletag.cmd.push(function() {' . "\n";
+
+ $js = '';
+ // As per Sync rendering docs, all options apart from enableServices should be wrapped in cmd.push
+ //https://support.google.com/dfp_premium/answer/1638622?hl=en#sync
+ $js .= 'googletag.cmd.push(function() {' . "\n";
+
if (variable_get('dfp_async_rendering', 1)) {
$js .= ' googletag.pubads().enableAsyncRendering();' . "\n";
}
@@ -721,18 +785,39 @@ function _dfp_js_global_settings() {
}
}
- $js .= '});' . "\n";
$js .= variable_get('dfp_injected_js2', '') . "\n";
+
+ // Closing googletag.cmd.push for sync rendering
+ if (!variable_get('dfp_async_rendering', 1)) {
+ $js .= '});' . "\n";
+ }
+
$js .= 'googletag.enableServices();';
- $options = array(
- 'type' => 'inline',
- 'group' => JS_DEFAULT,
- 'every_page' => TRUE,
- 'weight' => 10,
- 'force header' => TRUE,
- );
- drupal_add_js($js, $options);
+ // Closing googletag.cmd.push for async rendering
+ if (variable_get('dfp_async_rendering', 1)) {
+ $js .= '});' . "\n";
+
+ $options = array(
+ 'type' => 'inline',
+ 'group' => JS_DEFAULT,
+ 'every_page' => TRUE,
+ 'weight' => 10,
+ 'force header' => TRUE,
+ );
+ drupal_add_js($js, $options);
+ } else {
+ $element = array(
+ '#tag' => 'script',
+ '#weight' => 100000,
+ '#value' => $js,
+ '#attributes' => array(
+ 'type' => 'text/javascript'
+ ),
+ );
+
+ drupal_add_html_head($element, 'dfp_googletag_test');
+ }
}
/**
@@ -748,6 +833,10 @@ function _dfp_js_slot_definition($tag) {
// 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;
@@ -790,6 +879,11 @@ function _dfp_js_slot_definition($tag) {
if (!empty($tag->breakpoints)) {
$js .= ' .defineSizeMapping(mapping)' . "\n";
}
+ // Closing googletag.cmd.push
+ if (variable_get('dfp_async_rendering', 1)) {
+ $js .= ' })';
+ }
+
$js = rtrim($js, "\n") . ';';
$options = array(
@@ -798,6 +892,7 @@ function _dfp_js_slot_definition($tag) {
'weight' => 0,
'force header' => TRUE,
);
+
drupal_add_js($js, $options);
}