Skip to content

Commit

Permalink
added analytics events in newsletter
Browse files Browse the repository at this point in the history
  • Loading branch information
reemhamz committed Feb 6, 2024
1 parent 849f5ee commit 8a91b98
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions media/js/newsletter/newsletter-init.es6.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
/* eslint-disable no-console */
/*
* 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/.
*/
/* eslint-disable no-console */

import MzpNewsletter from '@mozilla-protocol/core/protocol/js/newsletter';

MzpNewsletter.init();
const successCustomCallback = () => {
const newsletters = Array.from(
document.querySelectorAll("input[name='newsletters']:checked")
).map((newsletter) => newsletter.value);

if (window.dataLayer) {
// UA
window.dataLayer.push({
event: 'newsletter-signup-success',
newsletter: newsletters
});
// GA4
for (let i = 0; i < newsletters.length; ++i) {
window.dataLayer.push({
event: 'newsletter_subscribe',
newsletter_id: newsletters[i]
});
}
}

// Glean
if (typeof window.Mozilla.Glean !== 'undefined') {
window.Mozilla.Glean.pageEvent({
label: 'newsletter-sign-up-success',
type: newsletters.join(', ')
});
}
};

const errorCustomCallback = (msg) => {
if (msg) {
console.log(msg);
}
};

MzpNewsletter.init(successCustomCallback, errorCustomCallback);

0 comments on commit 8a91b98

Please sign in to comment.