mirror of
https://github.com/vale981/Vulcan
synced 2025-03-09 12:16:37 -04:00
86 lines
2.4 KiB
JavaScript
86 lines
2.4 KiB
JavaScript
import { getSetting } from 'meteor/nova:core';
|
|
|
|
/*
|
|
|
|
We provide a special support for Google Analytics.
|
|
|
|
If you want to enable GA page viewing / tracking, go to
|
|
your settings file and update the "public > googleAnalyticsId"
|
|
field with your GA unique identifier (UA-xxx...).
|
|
|
|
*/
|
|
|
|
export const sendGoogleAnalyticsRequest = () => {
|
|
if (window && window.ga) {
|
|
window.ga('send', 'pageview', {
|
|
'page': window.location.pathname
|
|
});
|
|
}
|
|
};
|
|
|
|
export const initGoogleAnalytics = () => {
|
|
|
|
// get the google analytics id from the settings
|
|
const googleAnalyticsId = getSetting("googleAnalyticsId");
|
|
|
|
// the google analytics id exists & isn't the placeholder from sample_settings.json
|
|
if (googleAnalyticsId && googleAnalyticsId !== "foo123") {
|
|
|
|
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
|
|
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
|
|
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
|
|
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
|
|
|
|
const cookieDomain = document.domain === "localhost" ? "none" : "auto";
|
|
|
|
window.ga('create', googleAnalyticsId, cookieDomain);
|
|
|
|
// trigger first request once analytics are initialized
|
|
sendGoogleAnalyticsRequest();
|
|
}
|
|
};
|
|
|
|
/*
|
|
|
|
We provide a special support for Segment, using analytics-node
|
|
See https://segment.com/docs/sources/server/node/
|
|
|
|
*/
|
|
|
|
export const mutationAnalyticsAsync = (analytics, hook, document, user) => {
|
|
|
|
if (hook.includes('users')) {
|
|
// if the mutation is related to users, use analytics.identify
|
|
// see https://segment.com/docs/sources/server/node/#identify
|
|
|
|
// note: on users.new.async, user is undefined
|
|
const userId = user ? user._id : document._id;
|
|
|
|
const data = {
|
|
userId,
|
|
traits: document,
|
|
};
|
|
|
|
// uncomment for debug
|
|
// console.log(`// dispatching identify on "${hook}" (user ${userId})`);
|
|
// console.log(data);
|
|
|
|
analytics.identify(data);
|
|
|
|
} else {
|
|
// else use analytics.track
|
|
// see https://segment.com/docs/sources/server/node/#track
|
|
|
|
const data = {
|
|
userId: user._id,
|
|
event: hook,
|
|
properties: document,
|
|
};
|
|
|
|
// uncomment for debug
|
|
// console.log(`// dispatching track on "${hook}"`);
|
|
// console.log(data);
|
|
|
|
analytics.track(data);
|
|
}
|
|
}
|