Skip to content

Commit

Permalink
Merge pull request #3396 from alphagov/remove-ua
Browse files Browse the repository at this point in the history
Remove Universal Analytics
  • Loading branch information
AshGDS authored Jul 4, 2024
2 parents d0bfb59 + cf8202b commit 22d3b9f
Show file tree
Hide file tree
Showing 35 changed files with 45 additions and 850 deletions.
1 change: 0 additions & 1 deletion app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
//
//= require_tree ./analytics-ga4
//= require live_search
//= require search-analytics
//= require taxonomy-select
//= require_tree ./modules
//= require_tree ./components
Expand Down
108 changes: 3 additions & 105 deletions app/assets/javascripts/live_search.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,6 @@
this.bindSortElements()
this.getAndUpdateTaxonomyFacet()

if (window.ga) {
// Use navigator.sendBeacon
// https://developers.google.com/analytics/devguides/collection/analyticsjs/sending-hits#specifying_different_transport_mechanisms
window.ga('set', 'transport', 'beacon')
}

if (document.readyState === 'complete') {
this.Ga4EcommerceTracking()
} else {
Expand Down Expand Up @@ -93,10 +87,6 @@

if (e.keyCode === ENTER_KEY || e.type === 'change') {
// cater for jQuery and native events
var suppressAnalytics = e.suppressAnalytics || (e.detail && e.detail.suppressAnalytics)
if (e.currentTarget.value !== this.previousSearchTerm && !suppressAnalytics) {
LiveSearch.prototype.fireTextAnalyticsEvent(e)
}
this.formChange(e)
this.previousSearchTerm = e.currentTarget.value
e.preventDefault()
Expand All @@ -109,8 +99,6 @@
inputs[i].addEventListener('keypress', this.handleKeyPress.bind(this))
}

this.indexTrackingData()

document.addEventListener('popstate', this.popState.bind(this))
} else {
var fallback = this.$form.querySelector('.js-live-search-fallback')
Expand All @@ -119,19 +107,6 @@
}

LiveSearch.prototype.startEnhancedEcommerceTracking = function startEnhancedEcommerceTracking () {
if (this.$resultsWrapper) {
this.$resultsWrapper.setAttribute('data-search-query', this.currentKeywords())
var sortedBy = this.$resultsWrapper.querySelector('.js-order-results')
// Check that the sortedBy element exists and contains option elements
if (sortedBy && sortedBy.options.length > 0) {
this.$resultsWrapper.setAttribute('data-ecommerce-variant', sortedBy.options[sortedBy.selectedIndex].text)
}
}
if (this.$suggestionsBlock) {
this.$suggestionsBlock.setAttribute('data-search-query', this.currentKeywords())
}
if (GOVUK.Ecommerce) { GOVUK.Ecommerce.start() }

if (document.readyState === 'complete') {
this.Ga4EcommerceTracking(this.previousSearchUrl)
} else {
Expand Down Expand Up @@ -250,86 +225,19 @@
return params.join('&')
}

LiveSearch.prototype.setRelevantResultCustomDimension = function setRelevantResultCustomDimension () {
var $mostRelevantDocumentLink = this.$form.querySelector('.gem-c-document-list__item--highlight')
var dimensionValue = $mostRelevantDocumentLink ? 'yes' : 'no'
GOVUK.SearchAnalytics.setDimension(83, dimensionValue)
}

LiveSearch.prototype.trackingInit = function trackingInit () {
GOVUK.modules.start(this.$resultsWrapper)
this.indexTrackingData()
this.startEnhancedEcommerceTracking()
}

LiveSearch.prototype.trackPageView = function trackPageView () {
var newPath = window.location.pathname + '?' + this.serializeState(this.state)
GOVUK.SearchAnalytics.trackPageview(newPath)
GOVUK.SearchAnalytics.trackPageview(newPath, document.title, { trackerName: 'govuk' })
}

LiveSearch.prototype.trackSpellingSuggestionsImpressions = function trackSpellingSuggestionsImpressions ($suggestions) {
LiveSearch.prototype.updateSpellingSuggestionMetaTag = function updateSpellingSuggestionMetaTag () {
var $spellingSuggestionMetaTag = document.querySelector("meta[name='govuk:spelling-suggestion']")
if (this.$suggestionsBlock && $spellingSuggestionMetaTag) {
// currently there's ever only one suggestion
var spellingSuggestionAvailable = this.$suggestionsBlock.querySelector('a')
var suggestion = ''
if (spellingSuggestionAvailable) {
spellingSuggestionAvailable = JSON.parse(spellingSuggestionAvailable.getAttribute('data-track-options'))
suggestion = spellingSuggestionAvailable.dimension81
suggestion = spellingSuggestionAvailable.textContent
}
$spellingSuggestionMetaTag.setAttribute('content', suggestion)
}
}

/**
* Results grouped by facet and facet value do not have an accurate document index
* due to the post-search sorting and grouping which the presenter performs.
* In this case (ie. sorted by 'Topic' which actually means group by facet, facet value),
* rewrite the appropriate tracking data attribute to delineate the group and document index.
*/
LiveSearch.prototype.indexTrackingData = function indexTrackingData () {
var $groupEls = document.querySelectorAll('.filtered-results__group')
for (var g = 0; g < $groupEls.length; g++) {
var $resultEls = $groupEls[g].querySelectorAll('.gem-c-document-list__item')

for (var d = 0; d < $resultEls.length; d++) {
var $document = $resultEls[d]
var $documentLink = $document.querySelector('.gem-c-document-list__item-title')
var trackingAction = $documentLink.getAttribute('data-track-action')
trackingAction = trackingAction.replace(/\.\d+$/, '')
trackingAction = [trackingAction, g + 1, d + 1].join('.')
$documentLink.setAttribute('data-track-action', trackingAction)
}
}

var $results = document.querySelector('.js-finder-results')
if ($results) {
var $mostRelevantDocumentLink = $results.querySelector('.gem-c-document-list__item--highlight')

if ($mostRelevantDocumentLink) {
trackingAction = $mostRelevantDocumentLink.getAttribute('data-track-action')
trackingAction += 'r'
$mostRelevantDocumentLink.setAttribute('data-track-action', trackingAction)
}
}
}

LiveSearch.prototype.fireTextAnalyticsEvent = function fireTextAnalyticsEvent (event) {
var options = {
transport: 'beacon',
label: event.target.value
}
var category = 'filterClicked'
var action = document.querySelector('label[for="' + event.target.id + '"]').textContent

GOVUK.SearchAnalytics.trackEvent(
category,
action,
options
)
}

LiveSearch.prototype.cache = function cache (slug, data) {
if (typeof data === 'undefined') {
return this.resultCache[slug]
Expand Down Expand Up @@ -447,7 +355,6 @@
liveSearch.cache(liveSearch.serializeState(liveSearch.state), response)
liveSearch.ga4TrackFormChange(formChangeEvent) // must be before displayResults changes the DOM, otherwise will break formChangeEvent.target.closest
liveSearch.displayResults(response, searchState)
liveSearch.trackSearch()
} else {
liveSearch.showErrorIndicator()
}
Expand All @@ -461,7 +368,6 @@
this.updateUrl()
this.ga4TrackFormChange(formChangeEvent) // must be before displayResults changes the DOM, otherwise will break formChangeEvent.target.closest
this.displayResults(cachedResultData, searchState)
this.trackSearch()
}
}

Expand All @@ -470,12 +376,6 @@
window.history.pushState(this.state, '', newPath)
}

LiveSearch.prototype.trackSearch = function () {
this.trackingInit()
this.setRelevantResultCustomDimension()
this.trackPageView()
}

LiveSearch.prototype.updateLinks = function updateLinks () {
var searchState = '?' + this.serializeState(this.state)
if (typeof (this.emailSignupHref) !== 'undefined' && this.emailSignupHref != null) {
Expand Down Expand Up @@ -520,7 +420,7 @@
this.updateElement(this.$selectedFilterCount, results.display_selected_facets_count)
this.updateElement(this.$paginationBlock, results.next_and_prev_links)
this.updateElement(this.$suggestionsBlock, results.suggestions)
this.trackSpellingSuggestionsImpressions(results.suggestions)
this.updateSpellingSuggestionMetaTag()
this.updateSortOptions(results, action)
this.updateMeta(this.$resultsCountMetaTag, results.total)
this.updateMeta(this.$resultsTokenMetaTag, results.discovery_engine_attribution_token)
Expand All @@ -531,8 +431,6 @@
this.$loadingBlock.textContent = ''
this.$loadingBlock.style.display = 'none'
}
// send this event to notify the scroll tracker to reset, if listening
window.GOVUK.triggerEvent(this.$form, 'dynamic-page-update')
}

LiveSearch.prototype.restoreBooleans = function restoreBooleans () {
Expand Down
20 changes: 0 additions & 20 deletions app/assets/javascripts/modules/remove-filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};

var removeFilterName = $el.getAttribute('data-name')
var removeFilterValue = $el.getAttribute('data-value')
var removeFilterLabel = $el.getAttribute('data-track-label')
var removeFilterFacet = $el.getAttribute('data-facet')

var $input = this.getInput(removeFilterName, removeFilterValue, removeFilterFacet)
this.fireRemoveTagTrackingEvent(removeFilterLabel, removeFilterFacet)
this.clearFacet($input, removeFilterValue, removeFilterFacet)
}

Expand Down Expand Up @@ -75,24 +73,6 @@ window.GOVUK.Modules = window.GOVUK.Modules || {};
return element.querySelector(selector)
}

RemoveFilter.prototype.fireRemoveTagTrackingEvent = function (filterValue, filterFacet) {
var category = 'facetTagRemoved'
var action = filterFacet
var label = filterValue

GOVUK.SearchAnalytics.trackEvent(
category,
action,
{ label: label }
)

GOVUK.SearchAnalytics.trackEvent(
category,
action,
{ label: label, trackerName: 'govuk' }
)
}

RemoveFilter.prototype.decodeEntities = function (string) {
return string
.replace(/&quot;/g, '"')
Expand Down
27 changes: 0 additions & 27 deletions app/assets/javascripts/search-analytics.js

This file was deleted.

9 changes: 0 additions & 9 deletions app/models/checkbox_facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,6 @@ def id
"checkbox_facet-#{key}_#{value}"
end

def data
{
track_category: "filterClicked",
uncheck_track_category: "filterRemoved",
track_action: "checkboxFacet",
track_label: name,
}
end

def query_params
@checked ? { key => value } : {}
end
Expand Down
6 changes: 0 additions & 6 deletions app/models/option_select_facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,6 @@ def options(controls, key)
value: allowed_value["value"],
label: allowed_value["label"],
id: "#{key}-#{allowed_value['value']}",
data_attributes: {
track_category: "filterClicked",
uncheck_track_category: "filterRemoved",
track_action: name,
track_label: allowed_value["label"],
},
checked: selected_values.include?(allowed_value),
controls: controls || nil,
}
Expand Down
8 changes: 0 additions & 8 deletions app/models/taxon_facet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ def level_one_taxons
value: v["content_id"],
text: v["title"],
sub_topics: v["children"],
data_attributes: {
track_category: "filterClicked",
track_action: "level_one_taxon",
track_label: v["title"],
},
selected: v["content_id"] == @value_hash[LEVEL_ONE_TAXON_KEY],
}
end
Expand All @@ -92,9 +87,6 @@ def level_two_taxons
text: v["title"],
value: v["content_id"],
data_attributes: {
track_category: "filterClicked",
track_action: "level_two_taxon",
track_label: v["title"],
topic_parent: v["parent"],
},
selected: v["content_id"] == @value_hash[LEVEL_TWO_TAXON_KEY],
Expand Down
1 change: 0 additions & 1 deletion app/presenters/facet_tag_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ def present
data_facet: value["parameter_key"],
data_name: value["name"],
data_value: value["value"],
data_track_label: value["label"],
}
end
end
Expand Down
24 changes: 3 additions & 21 deletions app/presenters/search_result_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def document_list_component_data
text: title,
path: link,
description: sanitize(summary_text),
data_attributes: ga4_ecommerce_data(link).merge(ecommerce_data(link, title)),
data_attributes: ga4_ecommerce_data(link),
},
metadata: structure_metadata,
metadata_raw: metadata,
Expand Down Expand Up @@ -82,7 +82,7 @@ def include_parts?
end

def structure_parts
structured_parts = parts.map.with_index(1) do |part, index|
structured_parts = parts.map do |part|
has_required_data = %i[title slug body].all? { |key| part.key? key }
unless has_required_data
GovukError.notify(MalformedPartError.new, extra: { part:, link: })
Expand All @@ -94,7 +94,7 @@ def structure_parts
text: part[:title],
path:,
description: part[:body],
data_attributes: ga4_ecommerce_data(path).merge(ecommerce_data(path, part[:title], part_index: index)),
data_attributes: ga4_ecommerce_data(path),
},
}
end
Expand All @@ -110,23 +110,5 @@ def ga4_ecommerce_data(path)
}
end

def ecommerce_data(link, title, part_index: nil)
return {} unless @include_ecommerce

{
ecommerce_path: link,
ecommerce_row: 1,
ecommerce_index: document.index,
track_category: "navFinderLinkClicked",
track_action: [content_item.title, document.index, part_index].compact.join("."),
track_label: link,
track_options: {
dimension22: part_index,
dimension28: @count,
dimension29: title,
}.compact,
}
end

attr_reader :document, :metadata, :content_item, :result_number
end
12 changes: 1 addition & 11 deletions app/presenters/sort_option_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ def to_hash
{
label:,
value:,
data_track_category: "dropDownClicked",
data_track_action: "clicked",
data_track_label: label,
data_ga4_track_label: label,
selected:,
disabled:,
}
Expand All @@ -29,12 +27,4 @@ def to_hash
private

attr_reader :default, :selected, :disabled

def tracking_attributes
{
"data-track-category" => "dropDownClicked",
"data-track-action" => "clicked",
"data-track-label" => label,
}
end
end
Loading

0 comments on commit 22d3b9f

Please sign in to comment.