generated from 10up/plugin-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtracking.php
313 lines (284 loc) · 7.67 KB
/
tracking.php
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
/**
* JS tracking.
*
* @package SophiWP
*/
namespace SophiWP\Tracking;
use function SophiWP\Settings\get_sophi_settings;
use function SophiWP\Utils\get_domain;
use function SophiWP\Utils\get_section_name;
use function SophiWP\Utils\get_breadcrumb;
use function SophiWP\Utils\get_post_content_type;
use function SophiWP\Core\script_url;
use function SophiWP\Utils\get_wp_sophi_versions;
/**
* Default setup routine
*
* @return void
*/
function setup() {
$n = function( $function ) {
return __NAMESPACE__ . "\\$function";
};
add_action( 'wp_enqueue_scripts', $n( 'enqueue_scripts' ) );
add_filter( 'amp_analytics_entries', $n( 'amp_tracking' ) );
}
/**
* Enqueue tracking scripts.
*/
function enqueue_scripts() {
if ( ! page_need_tracking() ) {
return;
}
wp_enqueue_script(
'sophi-tag',
script_url( 'sophi-tag', 'frontend' ),
[],
SOPHI_WP_VERSION,
true
);
wp_localize_script(
'sophi-tag',
'SOPHIDATA',
get_tracking_data()
);
}
/**
* Add tracking to AMP pages.
*
* @param array $analytics_entries An associative array of the analytics entries we want to output.
*
* return array
*/
function amp_tracking( $analytics_entries ) {
if ( page_need_tracking() ) {
$analytics_entries[] = [
'type' => 'snowplow_v2',
'config' => wp_json_encode( get_amp_tracking_data() ),
];
}
return $analytics_entries;
}
/**
* Prepare data for JS tracking.
*/
function get_tracking_data() {
global $post;
$env = get_sophi_settings( 'environment' );
$data = [
'data' => [
'environment' => [
'environment' => $env,
'version' => get_wp_sophi_versions(),
],
'page' => [
'type' => is_singular() ? get_post_content_type( $post ) : 'section',
'breadcrumb' => get_breadcrumb(),
],
'content' => [
'type' => get_post_content_type( $post ),
],
],
'settings' => [
'client' => get_sophi_settings( 'tracker_client_id' ),
'tracker_address' => get_sophi_settings( 'tracker_address' ),
'appId' => sprintf( '%s:website', get_sophi_settings( 'tracker_client_id' ) ),
'collectorEndpoint' => get_sophi_settings( 'collector_url' ),
'linkedDomains' => [ get_domain() ],
'noConfigFile' => true,
'plugin' => [
'adblock' => false,
'private' => false,
'video' => true,
],
],
];
$section = get_section_name();
if ( $section ) {
$data['data']['page']['sectionName'] = $section;
}
if ( is_singular() ) {
$data['data']['content']['contentId'] = strval( $post->ID );
}
if ( is_front_page() || is_home() ) {
$data['data']['page']['breadcrumb'] = 'homepage';
$data['data']['page']['sectionName'] = 'homepage';
$data['data']['page']['type'] = 'section';
$data['data']['content']['type'] = 'section';
}
if ( 'prod' === $env ) {
$data['settings']['productionEndpoint'] = get_sophi_settings( 'collector_url' );
}
if ( 'stg' === $env ) {
$data['settings']['stagingEndpoint'] = get_sophi_settings( 'collector_url' );
}
/**
* Filter JS tracking data sent to Sophi Collector that gets generated on normal (non-AMP) pageviews. If you have a unique need and the default data doesn't match those needs, then you can utilize this filter to modify that data as needed.
*
* @since 1.0.0
* @hook sophi_tracking_data
*
* @param {array} $data JS tracking data.
*
* @return {array} JS tracking data.
*/
return apply_filters( 'sophi_tracking_data', $data );
}
/**
* Prepare data for AMP tracking.
*/
function get_amp_tracking_data() {
$data = [
'vars' => [
'collectorHost' => 'collector.sophi.io',
'appId' => sprintf( '%s:amp', get_sophi_settings( 'tracker_client_id' ) ),
'customContexts' => get_custom_contexts(),
],
'linkers' => [
'enabled' => true,
'proxyOnly' => false,
'destinationDomains' => get_domain(),
],
'triggers' => [
'defaultPageview' => [
'on' => 'visible',
'request' => 'pageView',
],
'trackFirstPagePing' => [
'on' => 'timer',
'request' => 'pagePing',
'timerSpec' => [
'interval' => 5,
'maxTimerLength' => 4.99,
'immediate' => false,
'startSpec' => [
'on' => 'visible',
'selector' => ':root',
],
'stopSpec' => [
'on' => 'hidden',
'selector' => ':root',
],
],
],
'trackPagePings' => [
'on' => 'timer',
'request' => 'pagePing',
'timerSpec' => [
'interval' => 20,
'maxTimerLength' => 1800,
'immediate' => false,
'startSpec' => [
'on' => 'visible',
'selector' => ':root',
],
'stopSpec' => [
'on' => 'hidden',
'selector' => ':root',
],
],
],
],
];
/**
* Filter AMP tracking data sent to Sophi Collector that gets generated on AMP pageviews. If you have a unique need and the default data doesn't match those needs, then you can utilize this filter to modify that data as needed.
*
* @since 1.0.0
* @hook sophi_amp_tracking_data
*
* @param {array} $data AMP tracking data.
*
* @return {array} AMP tracking data.
*/
return apply_filters( 'sophi_amp_tracking_data', $data );
}
/**
* Get custom context for AMP tracking.
*/
function get_custom_contexts() {
global $post;
$page_data = [
'type' => is_singular() ? get_post_content_type( $post ) : 'section',
'breadcrumb' => get_breadcrumb(),
'sectionName' => get_section_name(),
];
if ( is_singular() ) {
$content_data = [
'type' => get_post_content_type( $post ),
'contentId' => strval( $post->ID ),
];
$context = sprintf(
'%s,%s,%s',
wp_json_encode(
[
'schema' => '__environment_schama_url__',
'data' => [
'client' => get_sophi_settings( 'tracker_client_id' ),
'environment' => get_sophi_settings( 'environment' ),
'version' => get_wp_sophi_versions(),
],
]
),
wp_json_encode(
[
'schema' => '__page_schama_url__',
'data' => $page_data,
]
),
wp_json_encode(
[
'schema' => '__content_schama_url__',
'data' => $content_data,
]
)
);
} else {
$context = sprintf(
'%s,%s',
wp_json_encode(
[
'schema' => '__environment_schama_url__',
'data' => [
'client' => get_sophi_settings( 'tracker_client_id' ),
'environment' => get_sophi_settings( 'environment' ),
'version' => get_wp_sophi_versions(),
],
]
),
wp_json_encode(
[
'schema' => '__page_schama_url__',
'data' => $page_data,
]
),
);
}
// We need this indirect way to prevent the url encode two times.
return str_replace(
[ '__environment_schama_url__', '__page_schama_url__', '__content_schama_url__' ],
[ 'iglu:com.globeandmail/environment/jsonschema/1-0-9', 'iglu:com.globeandmail/page/jsonschema/1-0-10', 'iglu:com.globeandmail/content/jsonschema/1-0-12' ],
$context
);
}
/**
* Check if current page needs JS tracking. By default this will show on most every page (things like the 404 page will be excluded and if a site has a search page it will be excluded as well). If you have certain pages that you don't want tracked or pages that need to be tracked that aren't part of the default then this filter can be used to modify the default behavior.
*
* @return bool
*/
function page_need_tracking() {
/**
* Filter the type of page that needs tracking.
*
* @since 1.0.0
* @hook sophi_page_need_tracking
*
* @param {bool} $need_tracking Whether tracking should be enabled.
*
* @return {boold} Whether tracking should be enabled.
*/
return apply_filters(
'sophi_page_need_tracking',
is_tax() || is_tag() || is_category() || is_singular() || is_front_page() || is_home()
);
}