| |
| import { isStatCollectionEnabled } from "@/Plugins/Core/StatCollection/StatUtils"; |
| import { fetcher, ResponseType } from "./Fetcher"; |
| import * as GlobalVars from "@/Core/GlobalVars"; |
| import _ from "lodash"; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
| |
| |
| |
| async function injectGoogleAnalyticsScriptIfNeeded() { |
| if ((window as any).gtag !== undefined) { |
| |
| return; |
| } |
|
|
| await new Promise((resolve) => { |
| |
| const script = document.createElement("script"); |
| |
| 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 || []; |
| |
| |
| |
| window.gtag = function () { |
| (window as any).dataLayer.push(arguments); |
| }; |
|
|
| |
| |
| window.gtag("js", new Date()); |
|
|
| |
| |
| window.gtag("config", "G-FLN2FB201W"); |
|
|
| resolve(undefined); |
| }; |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| 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, |
| }); |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| 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); |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| export async function logEvent( |
| eventName: string, |
| eventAction: string |
| |
| ) { |
| |
| |
| _logInternal(eventName, eventAction); |
|
|
| |
| const url = window.location.href; |
| const gaEventData = `GA Event: ${eventName} - ${eventAction}`; |
|
|
| |
| |
| 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; |
| } |
| } |
| } |
|
|
| |
| |
| |
| |
| |
| if (!(await isStatCollectionEnabled())) { |
| |
| return; |
| } |
|
|
| _logGoogleAnalytics(eventName, eventAction, gaEventData); |
| } |
|
|