Skip to content

Instantly share code, notes, and snippets.

@sryan-mp
Created June 10, 2021 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sryan-mp/fc33b437526a3116cc15ffe2a5070790 to your computer and use it in GitHub Desktop.
Save sryan-mp/fc33b437526a3116cc15ffe2a5070790 to your computer and use it in GitHub Desktop.
import React from 'react';
import ReactGA from 'react-ga';
import isJson from 'utils/isJson';
export default class Analytics {
constructor(args) {
Object.assign(this, args);
this.scripts = scripts(args);
}
get mparticle() {
return this.scripts.mparticle;
}
}
export function gaPageView(path) {
ReactGA.set({ page: path });
ReactGA.pageview(path);
}
export function mpFormSubmitted(event) {
if (!isJson(event.data)) return;
const messageData = JSON.parse(event.data);
if (messageData.event === 'Form Submitted' && window.mParticle) {
const identityRequest = { userIdentities: { email: messageData.email } };
const identityCallback = (result) => {
if (result.getUser()) {
const user = result.getUser();
if (messageData.first_name) {
user.setUserAttribute('$FirstName', messageData.first_name);
}
if (messageData.last_name) {
user.setUserAttribute('$LastName', messageData.last_name);
}
if (messageData.company) {
user.setUserAttribute('Company', messageData.company);
}
if (messageData.job_title) {
user.setUserAttribute('Job Title', messageData.job_title);
}
if (messageData.role) {
user.setUserAttribute('Role', messageData.role);
}
window.mParticle.logEvent('Form Submitted', mParticle.EventType.Other, {
form_action: messageData.form_action,
first_name: messageData.first_name,
last_name: messageData.last_name,
email: messageData.email,
inquiry_type: messageData.inquiry_type,
partner_inquiry_type: messageData.partner_inquiry_type,
role: messageData.role,
message: messageData.message,
utm_campaign: messageData.utm_campaign,
utm_content: messageData.utm_content,
utm_medium: messageData.utm_medium,
utm_source: messageData.utm_source,
utm_term: messageData.utm_term,
});
}
};
window.mParticle.Identity.login(identityRequest, identityCallback);
}
}
export function mpPageView() {
if (window.mParticle) {
const path = window.location.pathname.toString();
const getCategory = (path) => {
if (path.includes('/platform/detail')) return 'Platform Detail Page';
if (path.includes('/platform/')) return 'Platform Pillar Page';
if (path.includes('/solutions/')) return 'Solutions Page';
if (path.includes('/integration/')) return 'Integration Page';
if (path.includes('/customers/')) return 'Customer Use Case';
if (path.includes('/resources/')) return 'Resources';
if (path.includes('/news/')) return 'News';
if (path.includes('/blog/')) return 'Blog Post';
if (path.includes('/lpg/')) return 'Landing Page';
if (path === '/') return 'Home';
return 'Other';
};
mParticle.logPageView(
'Marketing Page View',
{ anchor: path, category: getCategory(path) },
{ 'Google.Page': path }
);
}
}
export function logShare(network) {
if (window.mParticle) {
let title = document.title;
let URL = window.location.href.toString();
let utm_campaign, utm_medium, utm_source;
if (window.location.search) {
let queryString = window.location.search;
let urlParams = new URLSearchParams(queryString);
utm_campaign = urlParams.get('utm_campaign');
utm_source = urlParams.get('utm_source');
utm_medium = urlParams.get('utm_medium');
}
const event_props = {
network: network,
title: title,
url: URL,
};
if (utm_campaign) {
event_props.utm_campaign = utm_campaign;
}
if (utm_source) {
event_props.utm_source = utm_source;
}
if (utm_medium) {
event_props.utm_medium = utm_medium;
}
window.mParticle.logEvent(
'Share Button Clicked',
window.mParticle.EventType.Social,
event_props
);
}
}
export function mpContentDownloaded() {
console.log('test');
if (window.mParticle) {
if (window.location.search) {
let queryString = window.location.search;
let urlParams = new URLSearchParams(queryString);
console.log(urlParams)
}
window.mParticle.logEvent('Content Downloaded', window.mParticle.EventType.Other, {
content_name: contentName
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment