Sha256: 3ded620cfa0578975333fbcd4c4cd2ef0d7b64b5849fcd79ec200b2418907f8b

Contents?: true

Size: 1.71 KB

Versions: 78

Compression:

Stored size: 1.71 KB

Contents

window.GOVUK = window.GOVUK || {}
window.GOVUK.analyticsGa4 = window.GOVUK.analyticsGa4 || {}
window.GOVUK.analyticsGa4.analyticsModules = window.GOVUK.analyticsGa4.analyticsModules || {};

(function (analyticsModules) {
  'use strict'

  var Ga4CopyTracker = {
    init: function () {
      window.addEventListener('copy', this.trackCopy.bind(this))
    },

    trackCopy: function (event) {
      try {
        var text = this.getSelectedText()
        if (!text) {
          return // do nothing if no text has been selected
        }
        var target = event.target.tagName
        if (target === 'INPUT' || target === 'TEXTAREA') {
          return // do nothing if text is being copied from an input
        }

        var PIIRemover = new window.GOVUK.analyticsGa4.PIIRemover()
        text = window.GOVUK.analyticsGa4.core.trackFunctions.removeLinesAndExtraSpaces(text)
        text = PIIRemover.stripPIIWithOverride(text, true, true)
        var length = text.length
        text = text.substring(0, 30)

        var data = {
          event_name: 'copy',
          type: 'copy',
          action: 'copy',
          method: 'browser copy',
          text: text,
          length: length
        }
        window.GOVUK.analyticsGa4.core.applySchemaAndSendData(data, 'event_data')
      } catch (e) {
        console.warn('GA4 copy tracker error: ' + e.message, window.location)
      }
    },

    getSelectedText: function () {
      if (window.getSelection) {
        return window.getSelection().toString()
      } else if (document.selection) {
        return document.selection.createRange().text
      }
      return false
    }
  }

  analyticsModules.Ga4CopyTracker = Ga4CopyTracker
})(window.GOVUK.analyticsGa4.analyticsModules)

Version data entries

78 entries across 78 versions & 1 rubygems

Version Path
govuk_publishing_components-45.7.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-45.6.1 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-45.6.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-45.5.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-45.4.1 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-45.4.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-45.3.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-45.2.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-45.1.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-45.0.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-44.11.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-44.10.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-44.9.1 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-44.9.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-44.8.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-44.7.1 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-44.7.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-44.6.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-44.5.0 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js
govuk_publishing_components-44.4.2 app/assets/javascripts/govuk_publishing_components/analytics-ga4/ga4-copy-tracker.js