Skip to content

Commit

Permalink
Merge pull request #26 from xwp/fix/google-analytics-support
Browse files Browse the repository at this point in the history
Fix event error for GA, code clean up
  • Loading branch information
piotr-bajer authored May 25, 2021
2 parents cd36631 + 8a74ece commit 57c517a
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 16 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ add_theme_support( 'site_performance_tracker_vitals', array(

## Changelog

#### 0.6 - May 25, 2021

* Fix Google Analytics support.
* Code cleanup - remove unused metric and dimension.

#### 0.5 - April 13, 2021

* Feature: Add support for sending data in the web vitals report format.
Expand Down
2 changes: 1 addition & 1 deletion js/dist/module/web-vitals-analytics.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('wp-polyfill'), 'version' => '545643e091ef707f7501413bf114f4ab');
<?php return array('dependencies' => array('wp-polyfill'), 'version' => '13522fa45ebd348866b8c5c6566850db');
2 changes: 1 addition & 1 deletion js/dist/module/web-vitals-analytics.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions js/src/web-vitals-analytics.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const uaDimEventDebug = window.webVitalsAnalyticsData.eventDebug
? window.webVitalsAnalyticsData.eventDebug
: 'dimension6';

const measurementVersion = '6';

const getConfig = id => {
const config = {
page_path: location.pathname,
Expand All @@ -52,7 +54,7 @@ const getConfig = id => {
if ( 'gtag' === window.webVitalsAnalyticsData.delivery ) {
Object.assign( config, {
transport_type: 'beacon',
measurement_version: '6',
measurement_version: measurementVersion,
} );
}

Expand All @@ -67,16 +69,9 @@ const getConfig = id => {
[ uaDimConfig ]: 'config',
[ uaDimEventMeta ]: 'event_meta',
[ uaDimEventDebug ]: 'event_debug',
metric1: 'report_size',
},
} );
}

if ( 'ga' === window.webVitalsAnalyticsData.delivery ) {
Object.assign( config, {
[ uaDimMeasurementVersion ]: '6',
} );
}
}

return [ 'config', id, config ];
Expand Down Expand Up @@ -158,7 +153,6 @@ function sendToGoogleAnalytics( { name, value, delta, id, entries } ) {
value: Math.round( name === 'CLS' ? delta * 1000 : delta ),
non_interaction: true,
event_meta: getRating( value, vitalThresholds[ name ] ),
metric_rating: getRating( value, vitalThresholds[ name ] ),
event_debug: getDebugInfo( name, entries ),
} );
}
Expand All @@ -172,6 +166,7 @@ function sendToGoogleAnalytics( { name, value, delta, id, entries } ) {
transport: 'beacon',
[ uaDimEventMeta ]: getRating( value, vitalThresholds[ name ] ),
[ uaDimEventDebug ]: getDebugInfo( name, entries ),
[ uaDimMeasurementVersion ]: measurementVersion,
} );
}
}
Expand Down Expand Up @@ -208,9 +203,6 @@ export function initAnalytics() {
if ( 'undefined' === typeof window.ga ) {
// eslint-disable-next-line no-console
window.ga = console.log;
} else {
ga( 'js', new Date() );
ga( ...getConfig( window.webVitalsAnalyticsData.ga_id ) );
}
}

Expand Down
2 changes: 1 addition & 1 deletion php/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function inject_performance_observer() {
}
} );
var randNumber = Math.random();
if ( randNumber <= window.sitePerformanceObserver.chance ) {
if ( randNumber <= parseFloat( window.sitePerformanceObserver.chance ) ) {
window.sitePerformanceObserver.instance.observe( {
entryTypes: window.sitePerformanceObserver.entryTypes
} );
Expand Down
2 changes: 1 addition & 1 deletion site-performance-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* Plugin Name: Site Performance Tracker
* Plugin URI: https://github.com/xwp/site-performance-tracker
* Description: Allows you to detect and track site performance metrics.
* Version: 0.5
* Version: 0.6
* Author: XWP.co
* Author URI: https://xwp.co
*/
Expand Down

0 comments on commit 57c517a

Please sign in to comment.