Giter Club home page Giter Club logo

site-performance-tracker's Introduction

Site Performance Tracker

Test and Build

This WordPress plugin sends Core Web Vitals data to Google Analytics. It is compatible with Web Vitals Report

Installation

This plugin can be installed as a Composer dependency:

composer require xwp/site-performance-tracker

or by downloading a plugin ZIP file from the releases page.

Usage

The plugin must be configured by setting the site_performance_tracker_vitals theme feature with your Analytics IDs. Collected data will be available in Web Vitals Report in a few days.

To send Web Vitals metrics to Google Analytics in a format compatible with the Web Vitals Report, enable the following theme support and passing in the ID:

Google Analytics (analytics.js) is supported, requires passing the ID using ga_id:

add_theme_support( 'site_performance_tracker_vitals', array(
  'ga_id' => 'UA-XXXXXXXX-Y',
) );

Global Site Tag is supported, requires passing the Analytics ID (starting with UA- not GTM-) using gtag_id:

add_theme_support( 'site_performance_tracker_vitals', array(
  'gtag_id' => 'UA-XXXXXXXX-Y',
) );

GA4 Analytics is supported, requires passing the ID using ga4_id:

add_theme_support( 'site_performance_tracker_vitals', array(
  'ga4_id' => 'G-XXXXXXXXXX',
) );

If you need to override the Google Analytics dimensions (defaults to dimensions1 through dimension3) to store these under, pass them along on the add theme support initialisation:

add_theme_support( 'site_performance_tracker_vitals', array(
  'gtag_id'            => 'UA-XXXXXXXX-Y',
  'measurementVersion' => 'dimension7',
  'eventMeta'          => 'dimension8',
  'eventDebug'         => 'dimension9',
) );

The following hooks can be added to a theme or a custom plugin to configure the plugin, alternatively you can configure the plugin through the settings screen, in case of duplication, plugin will take programmatically set settings. To confirm they were applied look for the webVitalsAnalyticsData global variable in the page source.

Limit the number of events sent

The following filter can be used to limit the number of tracking events to a percentage of your traffic. For example, to limit the tracking events to 5% of requests, use the following logic:

add_filter( 'site_performance_tracker_chance', function() {
  return 0.05;
} );

Disable the tracking

Programmatically disable the plugin.

add_filter( 'site_performance_tracker_disabled', '__return_true' );

Delay script loading

Programmatically delay web vitals tracking to minimise impact on interactivity. By default, an idle callback request is postponed by 5000ms, a value which can be adjusted via a filter:

add_filter( 'site_performance_tracker_web_vitals_delay', function() {
  return 1000;
} );

Contribute

All contributions are welcome! Please create an issue for bugs and feature requests, and use pull requests for code contributions.

Project Setup

  • We use wp-env for local development environment. See all the env:* scripts in package.json for supported commands and helpers.

  • webpack.config.js configures how @wordpress/scripts transforms JS and CSS assets during packaging.

  • We use the @wordpress/eslint-plugin/recommended-with-formatting ruleset for JS linting since the Prettier integration is currently unreliable in @wordpress/scripts.

Changelog

1.3.2 - October 30th, 2023

  • Improved admin interface for GA4.
  • Bumped up build process to use Node 16.

1.3.1 - July 18th, 2023

  • Updated GA4 Settings.
  • Added WP-Rocket exclusion to web-vitals-analytics.
  • Cleaned up/refactored settings code.

1.3 - February 24th, 2023

  • Added INP metric.
  • Support for smaller web vitals ratio.

1.2 - December 5, 2022

  • Clean up settings code.

1.1.7 - July 5, 2022

  • Fix GA delivery bug.

1.1.6 - May 18, 2022

  • Fix GA not tracking bug when using the UI.

1.1.5 - March 17, 2022

  • Adding TTFB.

1.1.4 - March 10, 2022

  • Improve performance by loading and executing the script in quiter periods.
  • Make chunk hash a part of the output filename.

1.1.3 - March 9, 2022

  • Fix UI to prefill configured data.

1.1.2 - March 4, 2022

  • Fix duplicated page view when using gtag.

1.1.1 - March 3, 2022

  • Fix duplicitous page views when using gtag.
  • Fix PHP notices.

1.1.0 - January 6, 2022

  • Introduces an UI in WordPress Admin for easier configuration. If any config parameters are set in the theme files, the UI will not allow changing those parameters.

1.0.0 - October 4, 2021

  • Update docs to start the "Usage" section with the required configuration for the plugin to do anything.
  • Switch to basic PHP includes for loading the PHP files instead of Composer autoload.
  • Introduce helper methods for working with asset paths and URLs.
  • Introduce VIP Go coding standards.
  • Introduce PHP unit testing.

0.9.1 - July 9, 2021

  • Fix configureGtag call

0.9 - June 16, 2021

  • Update web vitals JS library to 2.0.1

0.8 - May 28, 2021

  • Remove Performance Observer functionality
  • Code cleanup

0.7 - May 26, 2021

  • Add support for Google Analytics 4.

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.

0.3.1 - March 11, 2020

  • Feature: Add support to Analytics added through Google Tag Managere.

0.3.0 - March 11, 2020

  • Feature: Track 'first-delay' of over 100ms.

0.2.0 - February 22, 2019

  • Make autoload.php optional to support project-wide autoload.
  • Add an action xwp/performance_tracker/render_mark as an alternative way for adding performance marks in the front-end.
  • Bugfix: Use proper JS escaping (as per WordPress VIP review).

0.1.1 - February 18, 2019

  • The plugin is no longer using a singleton pattern. Instead it is just a regular class that is being instantiated in the main plugin file.
  • Namespace has been added.
  • The PHP version check has been added (>= 5.3).
  • The helper functions are extracted to a separate file and they are now using static functions inside the class.
  • The $default_entry_types array is no longer defined as static.

0.1.0 - February 15, 2019

  • Initial release.

Contribute

Please follow the contribution guide.

Credits

Created by XWP and contributors. Licensed under GNU General Public License v2.0 or later.

site-performance-tracker's People

Contributors

akshitsethi avatar bharatpareek avatar delawski avatar dependabot[bot] avatar dero avatar kasparsd avatar ldanielgiuliani avatar loganwisniewski avatar manuelrod avatar mauu1t avatar mehigh avatar mykola avatar piotr-bajer avatar sabrina-zeidan avatar szepeviktor avatar tonnie-exelero avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

site-performance-tracker's Issues

Move plugin settings procedural code into a class

Move https://github.com/xwp/site-performance-tracker/blob/16931538f90de1d20a1c012f62cc854436dd4ec2/php/views/settings.php into something like php/src/class-settings.php which is bootstrapped during:

protected function register_hooks() {
/**
* Load web vitals analytics.
*/
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
/**
* Load styles for settings UI in Admin.
*/
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
/**
* Load only for modern browsers
*/
add_filter( 'script_loader_tag', array( $this, 'optimize_scripts' ), 10, 2 );
}

Fatal PHP error

When activating the plugin I get these errors:

Fatal error: Uncaught Error: Class 'Site_Performance_Tracker\Plugin' not found in /app/public/wp-content/plugins/site-performance-tracker-master/site-performance-tracker.php on line 60

Error: Class 'Site_Performance_Tracker\Plugin' not found in /app/public/wp-content/plugins/site-performance-tracker-master/site-performance-tracker.php on line 60

Let me know if I can help troubleshoot.

CLS - Send to debug the element that's prior to the largest shift

Right now the debug receives the largestSource.node, but most of the times this comes from a paragraph in the article content it makes it harder to identify which of the elements above it is the most problematic
https://github.com/xwp/site-performance-tracker/blob/master/js/src/web-vitals-analytics.js#L138
An idea is to look if the target node has an immediate previous sibbling, and if it exists, to deliver that instead to the debug data. This way we have the path to what might be quite the element causing the shift (or at the very least one step closer to it).

Bug: Interface in the UI

After the latest version (0.1.1+), the UI of the interface doesn't reflect the hard-coded values, showing the placeholders as if it wasn't already configured
Screenshot 2022-03-07 at 14 31 41

Disable custom dimensions for GA4

Currently, when GA4 Analytics is selected in the plugin settings, the three custom dimensions input fields are showing. With GA4, we don't need to set up the custom dimensions since they are already included. This is only needed when GA and GTM are selected.

Acceptance Criteria

  1. Do not show the following input fields in the settings UI when GA4 Analytics is selected under Analytics Types:
  • Measurement Version
  • Event Meta
  • Event Debug
  1. Ensure that when GA4 is selected, and inputs added, the settings can successfully save after removal of the custom dimensions input fields

Use web vitals attribution build for better INP tracking data

Fixes - https://xwp-co.atlassian.net/browse/INITS-2416

In a recent thread, there's a reference to the "attribution build" which can provide more information on the INP tracked information:
https://twitter.com/tunetheweb/status/1764757818303717863

We're loading the standard web-vitals library ourselves
https://github.com/xwp/site-performance-tracker/blob/master/js/src/measure-web-vitals.js#L2

As a side-effect of this, we'll need to update our switch to deliver the CLS / INP and LCP attribution instead of calculating ourselves "which" attribution should be reported:
https://github.com/xwp/site-performance-tracker/blob/master/js/src/send-to-analytics.js#L103-L140

Add CI Checks

We should lint PHP and potentially run tests on Travis to ensure that all code matches the coding standards.

Use hooks for adding markers

Using hooks such as do_action( 'site_performance_mark', 'position' ); instead of conditional methods calls would allow disabling the plugin on production and avoid any PHP errors if the plugin is not enabled.

Add a UI

Selecting from a dropdown between the 3 analytics types
Fill in the specific analytics ID
Depending on the selection, entering the numbers of the custom dimensions needed to send the debug data in.
Have an input to pick the ratio (0->1) of tracking for the web vitals.

Should contain a link to the web vitals report, and information surrounding the fact that the data range should only start for the dates when web vitals data is being sent.

Add an opt-in support for TTI metric polyfill

According to the Google Developer Guides the only way to track Time To Interactive (TTI) is to use a polyfill.

We should have a way to add the TTI metric along with adding the polyfill as an opt-in feature. I guess it could be another item in the $entry_types array. We currently filter this array using:

apply_filters( 'site_performance_tracker_event_types', array $entry_types = [ 'paint', 'navigation', 'mark' ] );

Bug: Minimise analytics side-effects

It looks like pageviews that fire the Web Vitals Event tend to report much higher Pages / Session. Any idea why that would be or if there’s something you can tweak to make it line up more with “normal” traffic patterns?

Seems like there should be a way to send the data without sending an extra pageview

Idea: maybe look at “non-interaction event” in GA, if it’s not already being sent that way

Avoid hard-coding thresholds

Once GoogleChrome/web-vitals#323 is merged and in a tagged release, we should move away from hard-coding thresholds in the plugin and instead reuse the exported thresholds directly. This ensures we wont miss an update to these thresholds and always measure against the latest recommendations.

Add installation instructions

The plugin currently must be installed as a Composer dependency and it won't work without the PSR-4 autoloader. We should update the readme to include clear installation instructions.

Annotation-based hooking

Would you welcome a PR that removes

add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );

and adds this?

/**
 * Enqueue javascript to trigger web vitals tracking.
 *
 * @hook wp_enqueue_scripts
 */
public function enqueue_scripts() {

Document Google Analytics requirements

Describe Google Analytics setup requirements (probably in the readme.md).

  • Do we currently only support sites that have ga() defined in the <head>?
  • Do we support the new gtag.js tags?

Not loading via Composer

The tracker code is not loading when installed via composer, as suggested in the readme.

When installed via WP Admin » Plugins, things work as expected.

Make autoload.php optional to support project-wide autoload

We should load the vendor/autoload.php only if it exists, to support projects that have root-level autoload and this plugin is part of the dependency list:

// Setup the Composer auto loader for classes.
require_once __DIR__ . '/vendor/autoload.php';

would become:

if ( file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
    require_once __DIR__ . '/vendor/autoload.php';
}

Allow smaller Web Vitals Tracking Ratio

The minimum Web Vitals Tracking Ratio possible right now is 0.01.
There are client's sites with enough visits to pass the recommended 30k tracked events per day using "0.01", therefore we bump into :
Screen Shot 2022-10-24 at 11 55 42 AM

Allow smaller ratios.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.