Sha256: 07e83a7166195ca3108c236db572df3019a64478240e52311885244c79e5d2be

Contents?: true

Size: 1.78 KB

Versions: 26

Compression:

Stored size: 1.78 KB

Contents

;(function (global) {
  'use strict'

  var $ = global.jQuery
  var GOVUK = global.GOVUK || {}

  GOVUK.analyticsPlugins = GOVUK.analyticsPlugins || {}
  GOVUK.analyticsPlugins.externalLinkTracker = function (options) {
    options = options || {}
    var externalLinkUploadCustomDimension = options.externalLinkUploadCustomDimension
    var currentHost = GOVUK.analyticsPlugins.externalLinkTracker.getHostname()
    var externalLinkSelector = 'a[href^="http"]:not(a[href*="' + currentHost + '"])'

    $('body').on('click', externalLinkSelector, trackClickEvent)

    function trackClickEvent (evt) {
      var $link = getLinkFromEvent(evt)
      var options = { transport: 'beacon' }
      var href = $link.attr('href')
      var linkText = $.trim($link.text())

      if (linkText) {
        options.label = linkText
      }

      if (externalLinkUploadCustomDimension !== undefined) {
        // This custom dimension will be used to duplicate the url information
        // that we normally send in an "event action". This will be used to join
        // up with a scheduled custom upload called "External Link Status".
        // We can only join uploads on custom dimensions, not on `event actions`
        // where we normally add the url info.
        var externalLinkToJoinUploadOn = href

        GOVUK.analytics.setDimension(externalLinkUploadCustomDimension, externalLinkToJoinUploadOn)
      }

      GOVUK.analytics.trackEvent('External Link Clicked', href, options)
    }

    function getLinkFromEvent (evt) {
      var $target = $(evt.target)

      if (!$target.is('a')) {
        $target = $target.parents('a')
      }

      return $target
    }
  }

  GOVUK.analyticsPlugins.externalLinkTracker.getHostname = function () {
    return global.location.hostname
  }

  global.GOVUK = GOVUK
})(window)

Version data entries

26 entries across 26 versions & 1 rubygems

Version Path
govuk_publishing_components-23.11.1 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.11.0 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.10.2 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.10.1 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.10.0 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.9.2 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.9.1 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.9.0 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.8.0 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.7.7 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.7.6 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.7.5 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.7.4 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.7.3 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.7.2 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.7.1 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.7.0 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.6.0 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.5.1 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js
govuk_publishing_components-23.5.0 app/assets/javascripts/govuk_publishing_components/analytics/external-link-tracker.js