diff --git a/bedrock/firefox/tests/test_helpers.py b/bedrock/firefox/tests/test_helpers.py
index b3884706ee2..c65d7093b6c 100644
--- a/bedrock/firefox/tests/test_helpers.py
+++ b/bedrock/firefox/tests/test_helpers.py
@@ -129,7 +129,7 @@ def test_download_location_attribute(self):
for link in links:
link = pq(link)
- assert link.attr("data-download-location") == "primary cta"
+ assert link.attr("data-cta-position") == "primary cta"
doc = pq(render("{{ download_firefox() }}", {"request": get_request, "fluent_l10n": self.get_l10n(get_request.locale)}))
@@ -137,7 +137,7 @@ def test_download_location_attribute(self):
for link in links[1:5]:
link = pq(link)
- assert link.attr("data-download-location") is None
+ assert link.attr("data-cta-position") is None
def test_download_nosnippet_attribute(self):
"""
@@ -339,7 +339,7 @@ def test_download_firefox_thanks_button(self):
assert href == "/firefox/download/thanks/"
assert button.attr("id") == "download-button-thanks"
- assert link.attr("data-link-type") == "download"
+ assert link.attr("data-cta-text") == "Download Firefox"
# Direct attribute for legacy IE browsers should always be win 32bit
assert link.attr("data-direct-link") == f"{settings.BOUNCER_URL}?product=firefox-stub&os=win&lang=en-US"
@@ -368,7 +368,7 @@ def test_download_firefox_thanks_attributes(self):
assert href == "/en-US/firefox/download/thanks/"
assert button.attr("id") == "test-download"
- assert link.attr("data-download-location") == "primary cta"
+ assert link.attr("data-cta-position") == "primary cta"
assert "test-css-class" in link.attr("class")
diff --git a/bedrock/firefox/tests/test_views.py b/bedrock/firefox/tests/test_views.py
index ed55fe0af8f..031bdbcfcb0 100644
--- a/bedrock/firefox/tests/test_views.py
+++ b/bedrock/firefox/tests/test_views.py
@@ -485,17 +485,14 @@ class TestFirefoxGA(TestCase):
def assert_ga_attr(self, response):
doc = pq(response.content)
links = doc(".mzp-c-button")
+ # test buttons all have appropriate attribute to trigger tracking in GA
for link in links.items():
- cta_data = link.attr("data-cta-type")
- cta_link = link.attr("data-link-type")
- if cta_data:
- contains_cta = any(cta_data in item for item in ["link", "button"])
- assert contains_cta or "fxa-" in cta_data
- elif cta_link:
- cta_link_types = ["download", "button", "link"]
- assert cta_link in cta_link_types
+ cta_text = link.attr("data-cta-text")
+ link_text = link.attr("data-link-text")
+ if cta_text or link_text:
+ assert True
else:
- assert False, f"{link} does not contain attr cta-type or link-type"
+ assert False, f"{link} does not contain attr data-cta-text or data-link-text"
def test_firefox_home_GA(self):
req = RequestFactory().get("/en-US/firefox/")
diff --git a/bedrock/mozorg/templates/mozorg/analytics-tests/ga-index.html b/bedrock/mozorg/templates/mozorg/analytics-tests/ga-index.html
index ab36d4276de..775bfe4360a 100644
--- a/bedrock/mozorg/templates/mozorg/analytics-tests/ga-index.html
+++ b/bedrock/mozorg/templates/mozorg/analytics-tests/ga-index.html
@@ -12,8 +12,8 @@
{% block content %}
This file is used only for automated testing for google tag manager
- download firefox
- click here to test
+ download firefox
+ click here to test
fxa button
{% endblock %}
diff --git a/media/css/firefox/browsers-products.scss b/media/css/firefox/browsers-products.scss
index 8f54490c215..6c9117340a6 100644
--- a/media/css/firefox/browsers-products.scss
+++ b/media/css/firefox/browsers-products.scss
@@ -296,7 +296,7 @@ $url-download-link-hover: svg-url($download-link-hover);
}
// issue 13317
-.fx-unsupported .cta-download[data-download-os="Desktop"]{
+.fx-unsupported .cta-download[data-cta-type="firefox"]{
display: none;
}
diff --git a/media/js/base/base-page-init.js b/media/js/base/base-page-init.js
index 4388fdbf417..e119f8bed77 100644
--- a/media/js/base/base-page-init.js
+++ b/media/js/base/base-page-init.js
@@ -15,7 +15,7 @@
var utils = Mozilla.Utils;
utils.initMobileDownloadLinks();
- utils.trackDownloadThanksButton();
+ utils.attributeDownloadThanksButton();
});
}
diff --git a/media/js/base/core-datalayer.js b/media/js/base/core-datalayer.js
new file mode 100644
index 00000000000..84bca868e18
--- /dev/null
+++ b/media/js/base/core-datalayer.js
@@ -0,0 +1,118 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+/**
+ * Utility class for core dataLayer object to track contextual user and page data
+ */
+
+if (typeof window.Mozilla.Analytics === 'undefined') {
+ window.Mozilla.Analytics = {};
+}
+
+(function () {
+ 'use strict';
+
+ var analytics = Mozilla.Analytics;
+ var isModernBrowser =
+ 'querySelector' in document && 'querySelectorAll' in document;
+
+ /** Returns whether page has download button.
+ * @param {String} path - URL path name fallback if page ID does not exist.
+ * @return {String} string.
+ */
+ analytics.pageHasDownload = function () {
+ if (!isModernBrowser) {
+ return 'false';
+ }
+ return document.querySelector('[data-download-version]') !== null
+ ? 'true'
+ : 'false';
+ };
+
+ /** Returns page version.
+ * @param {String} path - URL path name fallback if page ID does not exist.
+ * @return {String} version number from URL.
+ */
+ analytics.getPageVersion = function (path) {
+ var pathName = path ? path : document.location.pathname;
+ var versionResults = /firefox\/(\d+(?:\.\d+)?\.\da?\d?)/.exec(pathName);
+
+ return versionResults ? versionResults[1] : null;
+ };
+
+ /** Returns latest Fx version.
+ * @return {String} latest Fx version.
+ */
+ analytics.getLatestFxVersion = function () {
+ return document
+ .getElementsByTagName('html')[0]
+ .getAttribute('data-latest-firefox');
+ };
+
+ analytics.getAMOExperiment = function (params) {
+ var allowedExperiment = /^\d{8}_amo_.[\w/.%-]{1,50}$/; // should match the format YYYYMMDD_amo_experiment_name.
+ var allowedVariation = /^[\w/.%-]{1,50}$/; // allow alpha numeric & common URL encoded chars.
+
+ if (
+ Object.prototype.hasOwnProperty.call(params, 'experiment') &&
+ Object.prototype.hasOwnProperty.call(params, 'variation')
+ ) {
+ var experiment = decodeURIComponent(params['experiment']);
+ var variation = decodeURIComponent(params['variation']);
+
+ if (
+ allowedExperiment.test(experiment) &&
+ allowedVariation.test(variation)
+ ) {
+ return {
+ experiment: experiment,
+ variation: variation
+ };
+ }
+ }
+
+ return null;
+ };
+
+ /** Monkey patch for dataLayer.push
+ * Adds href stripped of locale to link click objects when pushed to the dataLayer,
+ * also removes protocol and host if same as parent page from href.
+ */
+ analytics.updateDataLayerPush = function (host) {
+ var dataLayer = (window.dataLayer = window.dataLayer || []);
+ var hostname = host || document.location.hostname;
+
+ dataLayer.defaultPush = dataLayer.push;
+ dataLayer.push = function () {
+ for (var i = 0; i < arguments.length; i++) {
+ if (arguments[i].event === 'gtm.linkClick') {
+ var element = arguments[i]['gtm.element'];
+ var href = element.href;
+
+ if (element.hostname === hostname) {
+ // remove host and locale from internal links
+ var path = href.replace(
+ /^(?:https?:\/\/)(?:[^/])*/,
+ ''
+ );
+ var locale = path.match(
+ /^(\/\w{2}-\w{2}\/|\/\w{2,3}\/)/
+ );
+
+ path = locale ? path.replace(locale[0], '/') : path;
+ arguments[i].newClickHref = path;
+ } else {
+ arguments[i].newClickHref = href;
+ }
+
+ dataLayer.defaultPush(arguments[i]);
+ } else {
+ dataLayer.defaultPush(arguments[i]);
+ }
+ }
+ };
+ };
+})();
diff --git a/media/js/base/mozilla-utils.js b/media/js/base/mozilla-utils.js
index 07135a39a69..fc19708d4c0 100644
--- a/media/js/base/mozilla-utils.js
+++ b/media/js/base/mozilla-utils.js
@@ -24,44 +24,31 @@ if (typeof window.Mozilla === 'undefined') {
};
/**
- * Get GA data attribute values for download_firefox_thanks() buttons.
+ * Get platform version for download_firefox_thanks() buttons.
* @param {Object} window.site
- * @returns {Object} os, name, version
+ * @returns {Object} version
*/
- Utils.getDownloadAttributionValues = function (site) {
+ Utils.getDownloadPlatformVersion = function (site) {
var data = {};
var platform = site.platform;
switch (platform) {
case 'windows':
- data.os = 'Desktop';
- data.name = 'Windows 32-bit';
data.version = 'win';
break;
case 'osx':
- data.os = 'Desktop';
- data.name = 'macOS';
data.version = 'osx';
break;
case 'linux':
- data.os = 'Desktop';
- data.name =
- site.archSize === 64 ? 'Linux 64-bit' : 'Linux 32-bit';
data.version = site.archSize === 64 ? 'linux64' : 'linux';
break;
case 'ios':
- data.os = 'iOS';
- data.name = 'iOS';
data.version = 'ios';
break;
case 'android':
- data.os = 'Android';
- data.name = 'Android';
data.version = 'android';
break;
default:
- data.os = 'Unsupported';
- data.name = 'Unsupported';
data.version = 'unsupported';
}
@@ -69,18 +56,16 @@ if (typeof window.Mozilla === 'undefined') {
};
/**
- * Set platfrom specific GA data attributes for download_firefox_thanks() buttons.
+ * Set stub attribution data attributes for download_firefox_thanks() buttons.
*/
- Utils.trackDownloadThanksButton = function () {
+ Utils.attributeDownloadThanksButton = function () {
var downloadButton = document.querySelectorAll(
'.c-button-download-thanks > .download-link'
);
- var data = Utils.getDownloadAttributionValues(window.site);
+ var data = Utils.getDownloadPlatformVersion(window.site);
for (var i = 0; i < downloadButton.length; ++i) {
- if (data && data.os && data.name && data.version) {
- downloadButton[i].setAttribute('data-download-os', data.os);
- downloadButton[i].setAttribute('data-display-name', data.name);
+ if (data && data.version) {
downloadButton[i].setAttribute(
'data-download-version',
data.version
diff --git a/tests/pages/firefox/family/landing.py b/tests/pages/firefox/family/landing.py
index 31f35428b28..0b4716e6e4b 100644
--- a/tests/pages/firefox/family/landing.py
+++ b/tests/pages/firefox/family/landing.py
@@ -14,9 +14,9 @@ class FamilyPage(BasePage):
_firefox_nav_cta_locator = (By.CSS_SELECTOR, ".c-navigation-shoulder")
- _firefox_nav_download_button_locator = (By.CSS_SELECTOR, "[data-download-location='nav']")
+ _firefox_nav_download_button_locator = (By.CSS_SELECTOR, "[data-cta-position='nav']")
- _firefox_desktop_download_button_locator = (By.CSS_SELECTOR, "[data-download-location='download section']")
+ _firefox_desktop_download_button_locator = (By.CSS_SELECTOR, "[data-cta-position='download section']")
_firefox_make_default_button_locator = (By.CSS_SELECTOR, "[data-cta-text='Set Firefox as your default browser']")
diff --git a/tests/unit/spec/base/core-datalayer.js b/tests/unit/spec/base/core-datalayer.js
new file mode 100644
index 00000000000..05bf73eec48
--- /dev/null
+++ b/tests/unit/spec/base/core-datalayer.js
@@ -0,0 +1,238 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+/* For reference read the Jasmine and Sinon docs
+ * Jasmine docs: https://jasmine.github.io/
+ * Sinon docs: http://sinonjs.org/docs/
+ */
+
+describe('core-datalayer.js', function () {
+ describe('pageHasDownload', function () {
+ it('will return "true" when download button is present on page.', function () {
+ const downloadMarkup =
+ '