GithubHelp home page GithubHelp logo

alphagov / gaap-analytics Goto Github PK

View Code? Open in Web Editor NEW
0.0 51.0 3.0 235 KB

Reusable scripts to make analytics easier

Home Page: https://governmentasaplatform.blog.gov.uk/

License: MIT License

JavaScript 100.00%
analytics event-tracking javascript karma-mocha

gaap-analytics's Introduction

Build Status npm

GaaP analytics tools

A bundle of reusable scripts to make adding custom events and pageviews to Google analytics. So far we have:

Event tracking

By including the script teams can easily add event listeners to clickable elements using just markup.

To get started add the script as a dependency with npm

npm i -S gaap-analytics

Then require the script within your app

const analytics = require('gaap-analytics')

analytics.eventTracking.init()

If you’re not using NPM then you can include the prebuilt version within build/

<script src="gaap-analytics.min.js"/>
<script>
    window.GAAP.analytics.eventTracking.init()
</script>

To trigger and configure the events, the following data attributes must be added to your markup.

  • data-click-events - this triggers the script and is designed to work with A, INPUT[type~="button radio checkbox"], BUTTON OR you can put it on a whole div/form and it will track all the aforementioned elements within it
  • data-click-category="Header" - this is the category GA will put it in
  • data-click-action="Navigation link clicked" - this is the action GA will use

This will set up Google Analytics event tracking. The label property will be inferred from element.value or element.innerText

Example

<nav data-click-events data-click-category="Header" data-click-action="Navigation link clicked">
  <ul>
    <li><a href="https://www.payments.service.gov.uk/#main" title="Learn more about GOV.UK Pay">About</a></li>
    <li><a href="https://docs.payments.service.gov.uk" title="Read the GOV.UK Pay Documentation">Documentation</a></li>
    <li><a href="https://www.payments.service.gov.uk/support/" title="Contact the GOV.UK Pay Team">Support</a></li>
    <li><a href="/login" title="Log me in" id="login" class="content login active">Sign in</a></li>
  </ul>
</nav>

The data attibutes attached to the <nav> element will trigger the JS to create eventListeners which will fire ga() events when clicked.

In the case the events will labelled up like so:

Category Action Label
Header Navigation link clicked About
Header Navigation link clicked Documentation
Header Navigation link clicked Support
Header Navigation link clicked Login

Virtual pageviews

Sometimes it’s useful to log something as a pageview rather than an event (if you’re making a funnel for a goal for instance). This script allows you to use data-attributes to make this happen, at the moment it only works for on form submit events. It can scrape your form a pick up values and post them as values to Google too.

To get started add the script as a dependency with npm

npm i -S gaap-analytics

Then require the script within your app

const analytics = require('gaap-analytics')

analytics.virtualPageview.init()

If you’re not using NPM then you can include the prebuilt version within build/

<script src="gaap-analytics.min.js"/>
<script>
    window.GAAP.analytics.virtualPageview.init()
</script>

To trigger and configure the virtual pageviews, the following data attributes must be added to your markup.

  • data-virtual-pageview="page/slug/name" - this triggers the script to run currently should only be added to a form
  • data-parameters="dimension1:service-name" you can add custom parameters to your virtual pageview, such as dimensions and metrics.
  • The dimension can be static or can be a name attribute of an element from your for. For multiple parameters, space separate them like data-parameters="dimension1:service-name metric1:total-amount"

Markup would look like this

  <form data-validate="true"data-virtual-pageview="/page/slug/name" data-dimensions="dimension1:service-name">
    <input id="service-name" name="service-name" type="text" value="">
    <button type="submit">Add service</button>
  </form>

gaap-analytics's People

Contributors

jonheslop avatar maxcbc avatar rhowe-gds avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.