File size: 5,252 Bytes
71174bc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | /* eslint-disable prefer-rest-params */
import { isStatCollectionEnabled } from "@/Plugins/Core/StatCollection/StatUtils";
import { fetcher, ResponseType } from "./Fetcher";
import * as GlobalVars from "@/Core/GlobalVars";
import _ from "lodash";
// declare global {
// interface Window {
// dataLayer: any[];
// gtag: (
// event: string,
// action: string,
// options?: Record<string, any>
// ) => void;
// }
// }
/**
* Injects the google analytics script if it hasn't been injected already. This
* is done to comply with the GDPR.
*/
async function injectGoogleAnalyticsScriptIfNeeded() {
if ((window as any).gtag !== undefined) {
// It's already inserted, so don't do it again.
return;
}
await new Promise((resolve) => {
// Insert google analytics script
const script = document.createElement("script");
// Bioti+e - GA4
script.src = `https://www.googletagmanager.com/gtag/js?id=G-FLN2FB201W`;
script.async = true;
document.head.appendChild(script);
script.onload = () => {
(window as any).dataLayer = (window as any).dataLayer || [];
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
// eslint-disable-next-line jsdoc/require-jsdoc
window.gtag = function () {
(window as any).dataLayer.push(arguments);
};
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.gtag("js", new Date());
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
window.gtag("config", "G-FLN2FB201W");
resolve(undefined);
};
});
}
/**
* Logs an event internally.
*
* @param {string} eventName The event name.
* @param {string} eventAction The event action.
*/
function _logInternal(eventName: string, eventAction: string) {
const formData = new FormData();
formData.append("e", `${eventName}-${eventAction}`);
formData.append("beta", (GlobalVars.isBeta || GlobalVars.isLocalHost) ? "1" : "0");
formData.append("test", GlobalVars.isTest ? "1" : "0");
fetcher("https://molmoda.org/e.php", {
responseType: ResponseType.TEXT,
formPostData: formData,
cacheBust: true,
});
}
/**
* Logs an event to Google Analytics. This function is asynchronous and will
* inject the Google Analytics script if it hasn't been injected already.
*
* @param {string} eventName The event name.
* @param {string} eventAction The event action.
* @param {string} gaEventData The event data to log.
*/
async function _logGoogleAnalytics(
eventName: string,
eventAction: string,
gaEventData: string
) {
await injectGoogleAnalyticsScriptIfNeeded();
if (
typeof window !== "undefined" &&
typeof (window as any).gtag === "function"
) {
console.log(gaEventData);
const eventActionToUse = `${eventName}-${eventAction}`;
(window as any).gtag("event", eventActionToUse);
}
}
/**
* Logs an event to Google Analytics.
*
* @param {string} eventName The event name.
* @param {string} eventAction The event action.
* @returns {void}
* @see
* https://developers.google.com/analytics/devguides/collection/gtagjs/events
*/
export async function logEvent(
eventName: string, // e.g., pluginId
eventAction: string // e.g., "jobSubmitted"
// eventOptions?: Record<string, any>
) {
// Always log IP internally for record keeping. It is anonymized client
// side, with minimal information stored. I believe it is compliant.
_logInternal(eventName, eventAction);
// If running from localhost, do a console log instead.
const url = window.location.href;
const gaEventData = `GA Event: ${eventName} - ${eventAction}`;
// If localhost, 127.0.0.1, or beta in the url, don't log to google
// analytics. Use "track_debug" to force logging regardless.
if (!url.includes("?track_debug")) {
if (GlobalVars.isLocalHost) {
console.warn(
`Analytics not sent from prohibited localhost domain: ${gaEventData}`
);
return;
}
const bannedUrls = ["?test=", "beta"];
for (const bannedUrl of bannedUrls) {
if (url.indexOf(bannedUrl) !== -1) {
console.warn(
`Analytics not sent from prohibited domain ${bannedUrl}: ${gaEventData}`
);
return;
}
}
}
// Cookies.get("statcollection") is a cookie that is set when the user
// accepts the cookie policy If the cookie is set, we load the google
// analytics script This is done to comply with the GDPR
// https://developers.google.com/analytics/devguides/collection/gtagjs/user-opt-out
// Cookies.get returns nothing if the cookie is not set
if (!(await isStatCollectionEnabled())) {
// Stat collection is disabled, so abandon effort.
return;
}
_logGoogleAnalytics(eventName, eventAction, gaEventData);
}
|