Skip to content

Commit

Permalink
Track Firefox downloads from /thanks in GA4
Browse files Browse the repository at this point in the history
review fixes
  • Loading branch information
stephaniehobson committed Jul 25, 2023
1 parent 106c30f commit b4fe423
Show file tree
Hide file tree
Showing 6 changed files with 189 additions and 163 deletions.
2 changes: 1 addition & 1 deletion media/js/firefox/all/all-downloads-unified-init.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/

import TrackProductDownload from '../../base/datalayer-productdownload.es6';
import MzpModal from '@mozilla-protocol/core/protocol/js/modal';
import MzpSideMenu from '@mozilla-protocol/core/protocol/js/sidemenu';
import TrackProductDownload from '../../base/datalayer-productdownload.es6';

(function (Mozilla) {
function onLoad() {
Expand Down
131 changes: 131 additions & 0 deletions media/js/firefox/new/common/thanks-direct.es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* 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/.
*/

import TrackProductDownload from '../../../base/datalayer-productdownload.es6';

let timeout;
let requestComplete = false;

function beginFirefoxDownload() {
const directDownloadLink = document.getElementById('direct-download-link');
let downloadURL;

// Only auto-start the download if a supported platform is detected.
if (
Mozilla.DownloadThanks.shouldAutoDownload(
window.site.platform,
window.site.fxSupported
) &&
typeof Mozilla.Utils !== 'undefined'
) {
downloadURL = Mozilla.DownloadThanks.getDownloadURL(window.site);

if (downloadURL) {
// Pull download link from the download button and add to the 'Try downloading again' link.
// Make sure the 'Try downloading again' link is well formatted! (issue 9615)
if (directDownloadLink && directDownloadLink.href) {
directDownloadLink.href = downloadURL;
directDownloadLink.addEventListener(
'click',
(event) => {
try {
TrackProductDownload.handleLink(event);
} catch (error) {
return;
}
},
false
);
}

// Start the platform-detected download a second after DOM ready event.
Mozilla.Utils.onDocumentReady(function () {
try {
TrackProductDownload.sendEventFromURL(downloadURL);
} catch (error) {
return;
}
setTimeout(function () {
window.location.href = downloadURL;
}, 1000);
});
}
}
}

function onSuccess() {
// Make sure we only initiate the download once!
clearTimeout(timeout);
if (requestComplete) {
return;
}
requestComplete = true;

// Fire GA event to log attribution success
window.dataLayer.push({
event: 'non-interaction',
eAction: 'direct-attribution',
eLabel: 'success'
});

beginFirefoxDownload();
}

function onTimeout() {
// Make sure we only initiate the download once!
clearTimeout(timeout);
if (requestComplete) {
return;
}
requestComplete = true;

// Fire GA event to log attribution timeout
window.dataLayer.push({
event: 'non-interaction',
eAction: 'direct-attribution',
eLabel: 'timeout'
});

beginFirefoxDownload();
}

/**
* If visitor already has a cookie, or does not meet the typical requirements fo
* stub attribution, then we can start the download as normal. If requirements *are*
* met and the visitor does *not* have a cookie, then attempt to make the attribution
* call before starting the download.
*/
if (
typeof Mozilla.StubAttribution !== 'undefined' &&
Mozilla.StubAttribution.meetsRequirements() &&
!Mozilla.StubAttribution.hasCookie()
) {
// Wait for GA to load so that we can pass along visit ID.
Mozilla.StubAttribution.waitForGoogleAnalytics(function () {
const data = Mozilla.StubAttribution.getAttributionData();

// make sure we check referrer for AMO (issue 11467)
if (
data &&
Mozilla.StubAttribution.withinAttributionRate() &&
Mozilla.StubAttribution.hasValidData(data)
) {
Mozilla.StubAttribution.successCallback = onSuccess;
Mozilla.StubAttribution.timeoutCallback = onTimeout;
// We don't want to delay the download indefinitely for a stub attribution call,
// so we only wait up to an additional 2 seconds (on top of GA) before downloading.
timeout = setTimeout(onTimeout, 2000);
Mozilla.StubAttribution.requestAuthentication(data);
} else {
beginFirefoxDownload();
}
});
} else {
beginFirefoxDownload();
}

// Bug 1354334 - add a hint for test automation that page has loaded.
document.getElementsByTagName('html')[0].classList.add('download-ready');
119 changes: 0 additions & 119 deletions media/js/firefox/new/common/thanks-direct.js

This file was deleted.

55 changes: 55 additions & 0 deletions media/js/firefox/new/common/thanks-init.es6.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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/.
*/

import TrackProductDownload from '../../../base/datalayer-productdownload.es6';

const directDownloadLink = document.getElementById('direct-download-link');
let downloadURL;

// Only auto-start the download if a supported platform is detected.
if (
Mozilla.DownloadThanks.shouldAutoDownload(
window.site.platform,
window.site.fxSupported
) &&
typeof Mozilla.Utils !== 'undefined'
) {
downloadURL = Mozilla.DownloadThanks.getDownloadURL(window.site);

if (downloadURL) {
// Pull download link from the download button and add to the 'Try downloading again' link.
// Make sure the 'Try downloading again' link is well formatted! (issue 9615)
if (directDownloadLink && directDownloadLink.href) {
directDownloadLink.href = downloadURL;
directDownloadLink.addEventListener(
'click',
(event) => {
try {
TrackProductDownload.handleLink(event);
} catch (error) {
return;
}
},
false
);
}

// Start the platform-detected download a second after DOM ready event.
Mozilla.Utils.onDocumentReady(function () {
try {
TrackProductDownload.sendEventFromURL(downloadURL);
} catch (error) {
return;
}
setTimeout(function () {
window.location.href = downloadURL;
}, 1000);
});
}
}

// Bug 1354334 - add a hint for test automation that page has loaded.
document.getElementsByTagName('html')[0].classList.add('download-ready');
41 changes: 0 additions & 41 deletions media/js/firefox/new/common/thanks-init.js

This file was deleted.

4 changes: 2 additions & 2 deletions media/static-bundles.json
Original file line number Diff line number Diff line change
Expand Up @@ -1385,14 +1385,14 @@
{
"files": [
"js/firefox/new/common/thanks.js",
"js/firefox/new/common/thanks-init.js"
"js/firefox/new/common/thanks-init.es6.js"
],
"name": "firefox_new_thanks"
},
{
"files": [
"js/firefox/new/common/thanks.js",
"js/firefox/new/common/thanks-direct.js"
"js/firefox/new/common/thanks-direct.es6.js"
],
"name": "firefox_new_thanks_direct"
},
Expand Down

0 comments on commit b4fe423

Please sign in to comment.