Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #814 from trufflesuite/feature/mixpanel-email
Browse files Browse the repository at this point in the history
Add email capture to MP analytics
  • Loading branch information
OnlyOneJMJQ authored Oct 30, 2020
2 parents 8877480 + e455fb4 commit f795d1a
Showing 1 changed file with 34 additions and 20 deletions.
54 changes: 34 additions & 20 deletions src/js/analytics/teams.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,3 @@
function getPort() {
// We grab this value from process.env.NODE_ENV
// It feels a bit dirty, but it's the best we can do with metalsmith
const environment = document.querySelector('meta[name="environment"]').getAttribute('content');

if (environment === 'dev' || environment === 'staging') {
return '2053';
}

if (environment === 'prod') {
return '2083';
}

// process.env.NODE_ENV should always be set, but in case of error fallback to dev
// so we don't pollute production data
return '2053';
}

// API Proxy
function mixpanelTrackProxy(eventName, eventParams) {
let httpRequest = new XMLHttpRequest();
Expand Down Expand Up @@ -44,8 +26,14 @@ function mixpanelTrackProxy(eventName, eventParams) {
* --------------
* First checks for a known campaign via the source query string.
* Falls back to document.referrer.
* Checks for email (if applicable) and sends that over if possible also.
*/
mixpanelTrackProxy("Page visit", {'source': getSource()});
if (getEmail()) {
let formattedEmail = getEmail().replace('%40', '@');
mixpanelTrackProxy("Page visit", {'source': getSource(), 'email': formattedEmail});
} else {
mixpanelTrackProxy("Page visit", {'source': getSource()});
}

// Navigation
Array.from(document.querySelectorAll('.subnav a')).forEach((link) => {
Expand Down Expand Up @@ -113,4 +101,30 @@ function getSource() {
}

return document.referrer;
};
};

function getEmail() {
const emailAddress = getQueryParam('email');

if (emailAddress.length > 0) {
return emailAddress;
}
};

function getPort() {
// We grab this value from process.env.NODE_ENV
// It feels a bit dirty, but it's the best we can do with metalsmith
const environment = document.querySelector('meta[name="environment"]').getAttribute('content');

if (environment === 'dev' || environment === 'staging') {
return '2053';
}

if (environment === 'prod') {
return '2083';
}

// process.env.NODE_ENV should always be set, but in case of error fallback to dev
// so we don't pollute production data
return '2053';
};

0 comments on commit f795d1a

Please sign in to comment.