hmrc / assets-frontend

Deprecated static assets for frontends on MDTP.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

stageprompt version included in minified application js doesn't cater for newer Google analytics.js

markhunter27 opened this issue · comments

From version 3.0.2+ the version of the stageprompt library that is being pulled in looks like an older version that doesn't cater for the newer Google Analytics API.
Causes stageprompt based analytics to fail when our service uses analytics.js

Problem/Feature

Stageprompt code is detecting elements with relevant data- attributes, but it's sending the event data to the global._gaq object - where the information accumulates but isn't processed.

          GOVUK.performance.sendGoogleAnalyticsEvent = function (category, event, label) {
            global._gaq.push(['_trackEvent', category, event, label, undefined, true])
          }

Expected behaviour

The later version of stageprompt has an revised flavour of the above function - caters for both versions of GA ..

        GOVUK.performance.sendGoogleAnalyticsEvent = function (category, event, label) {
          if (window.ga && typeof(window.ga) === 'function') {
            ga('send', 'event', category, event, label);
          } else {
            _gaq.push(['_trackEvent', category, event, label, undefined, true]);
          }
        };

Proposed solution

Within the package.json for AF, stageprompt was previously pulled directly from the node_module dependency, e.g. from v2.252.0 :
"stageprompt": "./node_modules/stageprompt/script/stageprompt.js",
..but in the later version it's getting pulled from the the govuk_frontend_toolkit dependency
"stageprompt": "./node_modules/govuk_frontend_toolkit/javascripts/stageprompt.js",
Possibly just need to switch back to the node module version?

We can patch the relevant function locally using this script, to work around the problem ..

    <script>
        document.addEventListener("DOMContentLoaded", function(event) {
            window.GOVUK.performance.sendGoogleAnalyticsEvent = function (category, event, label) {
              if (window.ga && typeof(window.ga) === 'function') {
                ga('send', 'event', category, event, label);
              } else {
                _gaq.push(['_trackEvent', category, event, label, undefined, true]);
              }
            };
            window.GOVUK.performance.stageprompt.setupForGoogleAnalytics();
        });
    </script>

Explain the steps to reproduce the bug

Bug happens whenever we use AF v3.0.2 with analytics.js - stageprompt events aren't being seen.

Tell us about your environment

  • Version used: AF v3.0.2 (with Google Analytics analytics.js)
  • Browser Name and version: Chrome 59.0.3071.115
  • Operating System and version (desktop or mobile): Ubuntu 14.04

Ah! This is why we were using our version which has that fix. Looks like we opted for the GOV.UK version when upgrading because the upstream of that fork has been archived.

IMO it would be better to get this added to the upstream version which has moved to GOV.UK frontend toolkit and bump our dependencies, rather than duplicate code again.

However, if this is blocking you @markhunter27 are you able to test moving back to our fork and raising an issue over on the upstream repo?

Actually, digging a bit further into this it looks like the analytics helpers have been expanded quite a bit since we forked alphagov/stageprompt.

The best thing to do would be to try to follow the govuk-frontend-toolkit analytics guidance (which includes universal analytics) and see if we need to add anything to AF as a result.

Cheers for the replies.
I think at the moment the most immediate solution for us is to use the local patch I mentioned above - that replicates the change that was made on the forked copy of stageprompt, and seems to do the job.

We can request a change to the GOV UK toolkit, but because we're one of several services using the 'Template as a service' approach, there's some time & impact we need to consider when stepping up the shared AF dependency version.

The later analytics docs you've shared look interesting & worth a look. But at the moment our service is using stageprompt attributes in a lot of places, so it won't be so quick to revise our approach.

Apologies @markhunter27, I didn't spot this yesterday but there's actually already an open PR for this change in govuk_frontend_template.

I'll revert back to our fork in the interim and will keep an eye on that.

No prob cheers. Caught up with @Fenwick17 this morning - will try to get a test of your latest AF happening in our service this afternoon.