From 0c5dcbf52963ced2a77ede90fafdc41c07468237 Mon Sep 17 00:00:00 2001 From: Ian James Date: Thu, 17 Jun 2021 12:59:58 +0100 Subject: [PATCH] Set LUX sampling to 1% LUX defaults to 100% of events being sent - setting this to 1% will reduce the number of events to the same level that has been set in Speedcurve's admin settings. --- CHANGELOG.md | 6 +++- .../vendor/lux/lux.js | 28 +++++++++++++++++-- docs/real-user-metrics.md | 20 ++++++++++++- 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7406db6225..6498133ed7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,11 @@ - Don't include changes that are purely internal. The CHANGELOG should be a useful summary for people upgrading their application, not a replication of the commit log. - + +## Unreleased + + * Set LUX's sampling rate to 1% ([PR 2145](https://github.com/alphagov/govuk_publishing_components/pull/2145)) + ## 24.14.1 * Fix wrong label value for event tracking on menu button ([PR #2139](https://github.com/alphagov/govuk_publishing_components/pull/2139)) diff --git a/app/assets/javascripts/govuk_publishing_components/vendor/lux/lux.js b/app/assets/javascripts/govuk_publishing_components/vendor/lux/lux.js index 352d3ae622..e6a7a83289 100644 --- a/app/assets/javascripts/govuk_publishing_components/vendor/lux/lux.js +++ b/app/assets/javascripts/govuk_publishing_components/vendor/lux/lux.js @@ -8,19 +8,32 @@ */ -/* ! Remember to keep the settings at the end of this file when updating LUX. +/* ! Remember to keep these settings at the end of this file when updating LUX: * - * The settings are: * * `LUX.customerid = 47044334` to let LUX know who this is * * `LUX.beaconMode = "simple"` to fire the beacon as an image, which is now * allowed by the content security policy. * * `LUX.debug = false` turns debugging on and off. Left set to false - and * kept in the file so it's easier to remember that this can be turned on. + * + * ! And the sample rate needs to be set inside the main function that's + * assigned to `LUX`: + +* * `LUX.samplerate = 1` to set sample rate to 1% of users. */ var LUX_t_start = Date.now(), LUX = window.LUX || {}; LUX = function() { + // ------------------------------------------------------------------------- + // Settings + // ------------------------------------------------------------------------- + // Set the sample rate to 1% to avoid all events being sent. + LUX.samplerate = 1 + // ------------------------------------------------------------------------- + /// End + // ------------------------------------------------------------------------- + var gaLog = []; dlog("lux.js evaluation start."); var version = "214", @@ -829,6 +842,10 @@ LUX = function() { }(); var LUX_t_end = Date.now(); +// ----------------------------------------------------------------------------- +// More settings +// ----------------------------------------------------------------------------- +// // This ID usually appended to the end of the lux.js as a query string when // using the SpeedCurve hosted version - but we have to include it here as this // is self hosted. @@ -842,3 +859,10 @@ LUX.beaconMode = "simple"; // `LUX.getDebug()` in the browser's console will show the history of what's // happened. LUX.debug = false; + +// Forces sampling - useful for when used with `debug = true` +// LUX.forceSample() + +// ----------------------------------------------------------------------------- +// End of more settings +// ----------------------------------------------------------------------------- diff --git a/docs/real-user-metrics.md b/docs/real-user-metrics.md index 31f5d34230..cbbe871eb6 100644 --- a/docs/real-user-metrics.md +++ b/docs/real-user-metrics.md @@ -40,7 +40,19 @@ Setting the beacon mode to `"simple"` turns the non-default image method on: LUX.beaconMode = 'simple' ``` -### Debug (bonus mode) +### Sample rate + +LUX defaults to sending every event to Speedcurve - this can be changed by setting `LUX.samplerate` to a integer: + +```javascript +LUX.samplerate = 1 // Whole number from 1 to 100. +``` + +This then only sends 1% of visits. + +**This must be set at the top of the main `LUX` function or it will default to 100% sample rate.** + +### Debugging (bonus mode) ```javascript LUX.debug = true @@ -53,6 +65,12 @@ Usefully, running `LUX.getDebug()` in the browser's console will show the histor LUX.getDebug() ``` +Sampling can also be forced by placing `LUX.forceSample()` at the end of the file: + +```javascript +LUX.forceSample() +``` + [Performance API]: https://developer.mozilla.org/en-US/docs/Web/API/Performance_API [LUX]: https://speedcurve.com/features/lux/