Skip to content

Commit

Permalink
Issue #110 "JS tracking and AMP compatibility" (#116)
Browse files Browse the repository at this point in the history
* Update class-statify-frontend.php

* Add filter for AMP analytics

* Update class-statify.php

* minor code style corrections (no semantics changed)

* do not append AMP analytics script if no JS tracking is used

* do not specity "type" attribute for AMP analytics script

There is no valid analytics vendor "statify" and we trigger a local
endpoint. To prevent AMP analtics from calling a vendor-endpoint with
404 result in this case, the type attribute is left empty.

* docs++

* adjust AMP analytics config to issue a POST request for WP-AJAX

Co-authored-by: Stefan Kalscheuer <[email protected]>
Co-authored-by: Torsten Landsiedel <[email protected]>
Co-authored-by: Patrick Robrecht <[email protected]>
  • Loading branch information
4 people authored Apr 21, 2020
1 parent 9c9fc3d commit 38ef7ed
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ All notable changes to this project will be documented in this file. This projec
* Add option to show total visits
* Refactored JavaScript tracking to use WP AJAX
* Updated Chartist JS library for dashboard widget
* Introduced new option to separate display from storage range
* Introduced new option to separate display from storage range
* Automatically add AMP analytics trigger if official AMP PlugIn is installed
* Dashboard widget is now scrollable to keep long-term statistics readable

## 1.6.3
Expand Down
48 changes: 47 additions & 1 deletion inc/class-statify-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static function track_visit( $is_snippet = false ) {
$use_snippet = self::$_options['snippet'];

// Set target & referrer.
$target = null;
$target = null;
$referrer = null;
if ( $use_snippet && $is_snippet ) {
if ( isset( $_REQUEST['statify_target'] ) ) {
Expand Down Expand Up @@ -390,4 +390,50 @@ public static function wp_footer() {
)
);
}

/**
* Add amp-analytics.
*
* @see https://amp-wp.org/documentation/playbooks/analytics/
*
* @param array $analytics Analytics.
*/
public static function amp_post_template_analytics( $analytics ) {
if ( ! is_array( $analytics ) ) {
$analytics = array();
}

// Analytics script is only relevant, if "JS" tracking is enabled, to prevent double tracking.
if ( self::$_options['snippet'] ) {
$analytics['statify'] = array(
'type' => '',
'attributes' => array(),
'config_data' => array(
'extraUrlParams' => array(
'action' => 'statify_track',
'_ajax_nonce' => wp_create_nonce( 'statify_track' ),
'statify_referrer' => '${documentReferrer}',
'statify_target' => '${canonicalPath}amp/',
),
'requests' => array(
'event' => admin_url( 'admin-ajax.php' ),
),
'triggers' => array(
'trackPageview' => array(
'on' => 'visible',
'request' => 'event',
'vars' => array(
'eventId' => 'pageview',
),
),
),
'transport' => array(
'xhrpost' => true,
),
),
);
}

return $analytics;
}
}
2 changes: 1 addition & 1 deletion inc/class-statify-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public static function options_snippet() {
<input id="statify-snippet" type="checkbox" name="statify[snippet]" value="1" <?php checked( Statify::$_options['snippet'], 1 ); ?>>
(<?php esc_html_e( 'Default', 'statify' ); ?>: <?php esc_html_e( 'No', 'statify' ); ?>)
<br>
<p class="description"><?php esc_html_e( 'This option is strongly recommended if caching is in use.', 'statify' ); ?></p>
<p class="description"><?php esc_html_e( 'This option is strongly recommended if caching or AMP is in use.', 'statify' ); ?></p>
<?php
}

Expand Down
3 changes: 3 additions & 0 deletions inc/class-statify.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ public static function init() {
add_action( 'template_redirect', array( 'Statify_Frontend', 'track_visit' ) );
add_filter( 'query_vars', array( 'Statify_Frontend', 'query_vars' ) );
add_action( 'wp_footer', array( 'Statify_Frontend', 'wp_footer' ) );
if ( function_exists( 'is_amp_endpoint' ) ) { // Automattic AMP plugin present.
add_filter( 'amp_post_template_analytics', array( 'Statify_Frontend', 'amp_post_template_analytics' ) );
}
}
}
}

0 comments on commit 38ef7ed

Please sign in to comment.