Skip to content

Commit

Permalink
Merge pull request #21 from xwp/feature/chance
Browse files Browse the repository at this point in the history
0.3.3 - Add possibility to report events only on % of traffic
  • Loading branch information
mehigh authored Mar 30, 2020
2 parents 4405610 + 44724ba commit d2cab66
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ if ( function_exists( 'get_the_performance_mark' ) ) {
}
```

### Limit the number of events sent

Using the following filter you can send the events for a limited percentage of your traffic, this limits the performance metrics to be sent only for 5% of the traffic:

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

### Hooks

There are the following hooks available to further customize the way the plugin works:
Expand Down
25 changes: 22 additions & 3 deletions php/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ class Plugin {
*/
protected $default_entry_types = array( 'paint', 'navigation', 'mark', 'first-input' );

/**
* PerformanceObserver default chance of sending performance metrics to analytics.
*
* @var array
*/
protected $default_chance = 1;

/**
* Initialize the plugin.
*/
Expand Down Expand Up @@ -87,10 +94,18 @@ public function inject_performance_observer() {
*/
$entry_types = apply_filters( 'site_performance_tracker_entry_types', $this->default_entry_types );

/**
* Limits the percentage of traffic chance of sending events to analytics.
*
* @param number $entry_chance Chance - a number between 0 and 1.
*/
$chance = apply_filters( 'site_performance_tracker_chance', $this->default_chance );

// Options object passed to JS.
$options = array(
'categoryName' => $category_name,
'entryTypes' => $entry_types,
'chance' => $chance,
);

?>
Expand Down Expand Up @@ -130,9 +145,13 @@ public function inject_performance_observer() {
}
}
} );
window.sitePerformanceObserver.instance.observe( {
entryTypes: window.sitePerformanceObserver.entryTypes
} );
var randNumber = Math.random();
if ( randNumber <= window.sitePerformanceObserver.chance ) {
window.sitePerformanceObserver.instance.observe( {
entryTypes: window.sitePerformanceObserver.entryTypes
} );
}

}
</script>
<?php
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.3.2
* Version: 0.3.3
* Author: XWP.co
* Author URI: https://xwp.co
*/
Expand Down

0 comments on commit d2cab66

Please sign in to comment.