Skip to content

Commit

Permalink
Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesCGDS committed Oct 25, 2022
1 parent b6775df commit 53c7039
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## Unreleased

* **BREAKING:** Fix referrer bug ([PR #2906](https://github.com/alphagov/finder-frontend/pull/2906))
* **BREAKING:** Fix referrer bug ([PR #3032](https://github.com/alphagov/govuk_publishing_components/pull/3032))

## 31.2.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
if (window.dataLayer) {
/* The dynamicPageViewData object is only passed to the init() function as a result of an AJAX request
(in live_search.js in the finder-frontend repository). Otherwise it will not be available and this indicates a fresh page load.
This is needed to trigger a fresh pageViewTracker push to the dataLayer on dynamic page updates (line 30) and to prevent multiple
click listeners from being applied (line 42). */
This is needed to trigger a fresh pageViewTracker push to the dataLayer on dynamic page updates and to prevent multiple
click listeners from being applied on this.searchResultsBlocks elements. */
var isNewPageLoad = !dynamicPageViewData

/* The data-ga4-ecommerce attribute may be present on several DOM elements e.g. search results and spelling
Expand Down
19 changes: 15 additions & 4 deletions docs/analytics-ga4/ga4-ecommerce-tracker.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,26 @@ In `live_search.js` in the `finder-frontend` repository (https://github.com/alph

```JavaScript
GOVUK.analyticsGa4.Ga4EcommerceTracker.init(dynamicPageViewData)

/* The init() function takes an object (dynamicPageViewData) as an optional parameter to detect whether dynamic updates have been made to the page via AJAX requests. The values in the object are then used to determine what is pushed to the dataLayer. If a parameter is not passed, this indicates a fresh page load (e.g. by refreshing the page or using the pagination) and that the page has not been dynamically updated via JS. */
```

The init() function takes an object (`dynamicPageViewData`) as an optional parameter to detect whether dynamic updates have been made to the page via AJAX requests. The values in the object are then used to determine what is pushed to the dataLayer. If a parameter is not passed, this indicates a fresh page load (e.g. by refreshing the page or using the pagination) and that the page has not been dynamically updated via JS.

Unlike the other GA4 tracking scripts in this repository, ecommerce tracking is not initialised by `init-ga4.js`. Instead, it is initialised by the JS that runs on finder pages (specifically, the `LiveSearch.prototype.Ga4EcommerceTracking()` function). It is called:

On page load (where no parameters are passed) - https://github.com/alphagov/finder-frontend/blob/main/app/assets/javascripts/live_search.js#L55
On page load (where no parameters are passed) e.g.

```
this.Ga4EcommerceTracking()
```

And whenever search results are updated (where an object parameter is passed) e.g.

And whenever search results are updated (where an object parameter is passed) - https://github.com/alphagov/finder-frontend/blob/main/app/assets/javascripts/live_search.js#L121
```
this.Ga4EcommerceTracking({
dynamic: 'true',
referrer: this.referrer
})
```

Note: ecommerce tracking will only run if there is an element which has the `data-ga4-ecommerce` attribute. If the script can't find an element with this attribute, it will stop running.

Expand Down

0 comments on commit 53c7039

Please sign in to comment.