(function() {
  "use strict";
  GOVUK.Tracker.load();
  var cookieDomain = (document.domain === 'www.beta.digitalmarketplace.service.gov.uk') ? '.digitalmarketplace.service.gov.uk' : document.domain;
  var property = (document.domain === 'www.digitalmarketplace.service.gov.uk') ? 'UA-49258698-1' : 'UA-49258698-3';
  GOVUK.analytics = new GOVUK.Tracker({
    universalId: property,
    cookieDomain: cookieDomain
  });
  GOVUK.analytics.trackPageview();
})();

(function() {
  "use strict";
  GOVUK.Tracker.load();
  var cookieDomain = (document.domain === 'www.beta.digitalmarketplace.service.gov.uk') ? '.digitalmarketplace.service.gov.uk' : document.domain;
  var property = (document.domain === 'www.digitalmarketplace.service.gov.uk') ? 'UA-49258698-1' : 'UA-49258698-3';
  GOVUK.analytics = new GOVUK.Tracker({
    universalId: property,
    cookieDomain: cookieDomain
  });
  GOVUK.analytics.trackPageview();

  // Tracking for declaration pages
  declarationPages = {
    'firstPage': '/suppliers/frameworks/g-cloud-7/declaration/g_cloud_7_essentials',
    'lastPage': '/suppliers/frameworks/g-cloud-7/declaration/grounds_for_discretionary_exclusion'
  };
  declarationVirtualPageViews = {
    'firstQuestion': declarationPages.firstPage + '/interactions/accept-terms-of-participation',
    'makeDeclarationButton': declarationPages.lastPage + '/interactions/make-declaration'
  };

  var registerFirstQuestionInteraction = function (e) {
    var $target;

    $target = $(e.target);

    // if the click target is a child node of the label, get the label instead
    if ($target.prop('nodeName').toLowerCase() !== 'label') {
      $target = $target.closest('label');
    }

    GOVUK.analytics.trackPageview(declarationVirtualPageViews.firstQuestion + '/yes');
    GOVUK.analytics.trackPageview(declarationVirtualPageViews.firstQuestion + '/no');
  };

  var registerDeclarationMade = function (e) {
    if (window.location.pathname === declarationPages.lastPage) {
      GOVUK.analytics.trackPageview(declarationVirtualPageViews.makeDeclarationButton);
    }
  }

  if (window.location.pathname === declarationPages.firstPage) {
    $('body').on('click', 'form.supplier-declaration fieldset:eq(0) label.selection-button', registerFirstQuestionInteraction);
  }
  if (window.location.pathname === declarationPages.lastPage) {
    $('form.supplier-declaration .button-save').on('click', registerDeclarationMade);
  }
})();

Add tracking for declaration interactions
Add tracking for declaration interactions

Uses virtual page views because they are the only
form of interaction Google Analytics can track
which appears in conversion funnels.

We are interested in knowing:

1. if visitors to the page interact with the form
2. if they complete the declaration*

*this would normally be done by looking at the
next URL but completing the declaration takes you
back to the dashboard which has the same URL as it
does normally.
