Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
old ga index
import * as dimensions from "./dimensions";
import * as environment from "../../util/env";
import * as dom from "../../util/dom";
import * as gtm from "./gtm";
import trackerFactory from "../../gdpr/trackerFactory";
import gdprConsented from "../../gdpr/consented";
import campaigns from "mashkit-campaigns";
import {campaigns as campaignConfig} from './config';
const AMP_ID = "UA-92124-48";
const PROD_ID = "UA-92124-1";
const DEV_ID = "UA-116120481-7";
const STAGING_ID = "UA-116120481-7";
const PCMAG_ID = "UA-3705233-1";
const GEEK_ID = "UA-21677019-1";
const ANALYTICS_PATH = "https://www.google-analytics.com/analytics.js";
const GA_TIMER = 100;
const GDPR_NO_CONSENT_EVENT = "evidonNoConsentGiven";
export const ANALYTICS_GLOBAL = "mashkitGA";
export const TRACKER_NAME = "MashKitTracker";
export let loaded = false;
export let UAObj = null;
let gaLogsType = null;
export function loadGoogleAnalytics() {
return new Promise((resolve) => {
if (dom.globals[ANALYTICS_GLOBAL]) {
return resolve(dom.globals[ANALYTICS_GLOBAL]);
}
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r;
i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments);
setTimeout(() => {
logPushEvent(arguments)
}, 0);
}, 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', ANALYTICS_PATH, ANALYTICS_GLOBAL);
const timer = setInterval(() => {
if (dom.globals[ANALYTICS_GLOBAL]) {
clearInterval(timer);
campaigns.initialize(campaignConfig, exports.event);
resolve(dom.globals[ANALYTICS_GLOBAL]);
}
}, GA_TIMER);
});
}
export function getPropertyID() {
const {pathname, search} = dom.globals.document.location;
if (search && search.includes("player=pcmag")) {
return PCMAG_ID;
}
if (search && search.includes("player=geek")) {
return GEEK_ID;
}
if (pathname.includes(".amp") && environment.isProduction()) {
return AMP_ID;
}
if (environment.isProduction()) {
return PROD_ID;
}
return DEV_ID;
}
export function setUp(ga) {
if (!exports.loaded) {
const dataLayer = dom.globals.dataLayer = dom.globals.dataLayer || [];
ga("create", exports.getPropertyID(), "auto", TRACKER_NAME);
if (!gdprConsented()) {
const pushedData = {
event: GDPR_NO_CONSENT_EVENT,
anonymizeIP: !gdprConsented()
};
dataLayer.push(pushedData);
setTimeout(() => {
logPushEvent(pushedData)
}, 0);
}
exports.UAObj = ga;
exports.loaded = true;
if ((dom.globals.window) && (dom.globals.window.location.search.indexOf(`loggaevents=1`) !== -1))
gaLogsType = 1;
else if ((dom.globals.window) && (dom.globals.window.location.search.indexOf(`loggaevents=2`) !== -1))
gaLogsType = 2;
setTimeout(() => {
logInitEvent(['ga setup']);
}, 0);
}
return exports.UAObj;
}
export function getUA() {
return exports.loadGoogleAnalytics().then((ga) => {
return exports.setUp(ga);
});
}
export function pageview(data = {}, dimensions = []) {
const payload = exports.default.dimensions.payload(data, dimensions);
exports.getUA().then(() => {
exports.UAObj(`${TRACKER_NAME}.send`, "pageview", payload);
});
}
export function event(category, action, label = undefined, data = {}, dimensions = []) {
const args = arguments;
setTimeout(() => {
logInitEvent(args);
}, 0);
const payload = exports.default.dimensions.payload(data, dimensions);
exports.getUA().then(() => {
exports.UAObj(`${TRACKER_NAME}.send`, Object.assign({
hitType: "event",
eventCategory: category,
eventAction: action,
eventLabel: label
}, payload));
});
}
export function pushEvent(event, data = {}, dimensions = [], categoryActionLabel = {}) {
const args = arguments;
setTimeout(() => {
logInitEvent(args);
}, 0);
const payload = exports.default.gtm.payload(data, dimensions);
exports.getUA().then(() => {
const dataLayer = dom.globals.dataLayer = dom.globals.dataLayer || [];
const pushedData = Object.assign({}, payload, {event}, categoryActionLabel);
dataLayer.push(pushedData);
setTimeout(() => {
logPushEvent(pushedData)
}, 0);
});
}
function logInitEvent(args) {
if (gaLogsType === 1) {
console.log('GA init event', {o: dom.globals.__o, args: args, location: dom.globals.document.location});
}
else if (gaLogsType === 2) {
console.log(`GA init event ${JSON.stringify({
o: dom.globals.__o,
args: args,
location: {
hostname: dom.globals.document.location.hostname,
pathname: dom.globals.document.location.pathname
}
})}`);
}
}
function logPushEvent(pushedData) {
if (gaLogsType === 1) {
console.log('GA push event log', pushedData);
}
else if (gaLogsType === 2) {
console.log(`GA push event log ${JSON.stringify(pushedData)}`)
}
}
export function setAttribute(attribute, value) {
exports.getUA().then(() => {
exports.UAObj(`${TRACKER_NAME}.set`, attribute, value);
});
}
export default {
loaded,
getUA,
setUp,
dimensions,
pageview,
event,
UAObj,
gtm,
pushEvent,
setAttribute,
campaigns,
ANALYTICS_GLOBAL,
TRACKER_NAME,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.