@@ -374,8 +374,12 @@ public static function query_vars( $vars ) {
374
374
* @version 1.4.1
375
375
*/
376
376
public static function wp_footer () {
377
- // Skip by option.
378
- if ( ! self ::is_javascript_tracking_enabled () ) {
377
+ // JS tracking disabled or AMP is used for the current request.
378
+ if (
379
+ ! self ::is_javascript_tracking_enabled () ||
380
+ ( function_exists ( 'amp_is_request ' ) && amp_is_request () ) ||
381
+ ( function_exists ( 'is_amp_endpoint ' ) && is_amp_endpoint () )
382
+ ) {
379
383
return ;
380
384
}
381
385
@@ -404,7 +408,30 @@ public static function wp_footer() {
404
408
}
405
409
406
410
/**
407
- * Add amp-analytics.
411
+ * Add amp-analytics for Standard and Transitional mode.
412
+ *
413
+ * @see https://amp-wp.org/documentation/playbooks/analytics/
414
+ *
415
+ * @param array $analytics_entries Analytics entries.
416
+ */
417
+ public static function amp_analytics_entries ( $ analytics_entries ) {
418
+ if ( ! is_array ( $ analytics_entries ) ) {
419
+ $ analytics_entries = array ();
420
+ }
421
+
422
+ // Analytics script is only relevant, if "JS" tracking is enabled, to prevent double tracking.
423
+ if ( self ::is_javascript_tracking_enabled () ) {
424
+ $ analytics_entries ['statify ' ] = array (
425
+ 'type ' => '' ,
426
+ 'config ' => wp_json_encode ( self ::make_amp_config () ),
427
+ );
428
+ }
429
+
430
+ return $ analytics_entries ;
431
+ }
432
+
433
+ /**
434
+ * Add AMP-analytics for Reader mode.
408
435
*
409
436
* @see https://amp-wp.org/documentation/playbooks/analytics/
410
437
*
@@ -420,32 +447,40 @@ public static function amp_post_template_analytics( $analytics ) {
420
447
$ analytics ['statify ' ] = array (
421
448
'type ' => '' ,
422
449
'attributes ' => array (),
423
- 'config_data ' => array (
424
- 'extraUrlParams ' => array (
425
- 'action ' => 'statify_track ' ,
426
- '_ajax_nonce ' => wp_create_nonce ( 'statify_track ' ),
427
- 'statify_referrer ' => '${documentReferrer} ' ,
428
- 'statify_target ' => '${canonicalPath}amp/ ' ,
429
- ),
430
- 'requests ' => array (
431
- 'event ' => admin_url ( 'admin-ajax.php ' ),
432
- ),
433
- 'triggers ' => array (
434
- 'trackPageview ' => array (
435
- 'on ' => 'visible ' ,
436
- 'request ' => 'event ' ,
437
- 'vars ' => array (
438
- 'eventId ' => 'pageview ' ,
439
- ),
440
- ),
441
- ),
442
- 'transport ' => array (
443
- 'xhrpost ' => true ,
444
- ),
445
- ),
450
+ 'config_data ' => self ::make_amp_config (),
446
451
);
447
452
}
448
453
449
454
return $ analytics ;
450
455
}
456
+
457
+ /**
458
+ * Generate AMP-analytics configuration.
459
+ *
460
+ * @return array Configuration array.
461
+ */
462
+ private static function make_amp_config () {
463
+ return array (
464
+ 'requests ' => array (
465
+ 'pageview ' => admin_url ( 'admin-ajax.php ' ),
466
+ ),
467
+ 'extraUrlParams ' => array (
468
+ 'action ' => 'statify_track ' ,
469
+ '_ajax_nonce ' => wp_create_nonce ( 'statify_track ' ),
470
+ 'statify_referrer ' => '${documentReferrer} ' ,
471
+ 'statify_target ' => '${canonicalPath}amp/ ' ,
472
+ ),
473
+ 'triggers ' => array (
474
+ 'trackPageview ' => array (
475
+ 'on ' => 'visible ' ,
476
+ 'request ' => 'pageview ' ,
477
+ ),
478
+ ),
479
+ 'transport ' => array (
480
+ 'beacon ' => true ,
481
+ 'xhrpost ' => true ,
482
+ 'image ' => false ,
483
+ ),
484
+ );
485
+ }
451
486
}
0 commit comments