Skip to content

Commit

Permalink
Fix error in lux script
Browse files Browse the repository at this point in the history
- to reproduce, clear cookies, reload page then accept cookies, error of `Uncaught TypeError: Cannot read properties of null (reading 'pop')` will occur
- this refers to the `getCustomerId()` function, which if `LUX.customerid` is not defined will attempt to determine it from the source URL of the LUX script, something like `lux.js?id=1234`
- we don't get our customerid this way, instead we hard code it into this file, because we host our own copy of the LUX script (see the documentation for more detail and background on this), so the script failed, because there was nothing to find
- instead we remove this part of the `getCustomerId()` function and simply return `LUX.customerid`. We also now need to set this inside the LUX object itself, because otherwise it isn't set properly in the steps described above
- documentation updated to reflect this for future updates
  • Loading branch information
andysellick committed Sep 6, 2023
1 parent 076f974 commit bb599c2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -9,6 +9,7 @@

## Unreleased

* Fix error in LUX script ([PR #3592](https://github.com/alphagov/govuk_publishing_components/pull/3592))
* Prepend page path when tracking anchor links in GA4 ([PR #3590](https://github.com/alphagov/govuk_publishing_components/pull/3590))

## 35.15.1
Expand Down

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

4 changes: 2 additions & 2 deletions docs/real-user-metrics.md
Expand Up @@ -28,9 +28,9 @@ The customer ID is an identifier for the site using LUX, not for the user visiti

When loading `lux.js` from SpeedCurve's CDN, the customer ID is appended to the end of the URI as a query string. The script looks for a script in the DOM with a source of `lux.js`, and from that extracts the customer ID.

Rails adds a fingerprint to the URI which means that `lux.js` becomes (for example) `lux.self-7137780d5344a93190a2c698cd660619d4197420b9b1ef963b639a825a6aa5ff.js` and the script can't find itself.
Rails adds a fingerprint to the URI which means that `lux.js` becomes (for example) `lux.self-7137780d5344a93190a2c698cd660619d4197420b9b1ef963b639a825a6aa5ff.js` and the script can't find itself. Because of this that part of the script would fail. Instead, we modify the `getCustomerId()` function to simply return `LUX.customerid`.

Because of this the customer ID needs to be set at the end of the `lux.js` file:
Because of this the customer ID needs to be set in the `lux.js` file:

```javascript
LUX.customerid = 47044334
Expand Down

0 comments on commit bb599c2

Please sign in to comment.