Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
141 lines
4.4 KiB
TypeScript
141 lines
4.4 KiB
TypeScript
import { useSetting, useUserId } from '@rocket.chat/ui-contexts';
|
|
import { useEffect } from 'react';
|
|
|
|
declare global {
|
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
interface Window {
|
|
_paq?: [string, ...unknown[]][];
|
|
GoogleAnalyticsObject: unknown;
|
|
ga?: qa;
|
|
}
|
|
|
|
type qa = {
|
|
(...args: unknown[]): void;
|
|
l?: number;
|
|
q?: unknown[];
|
|
};
|
|
}
|
|
|
|
export const useAnalytics = (): void => {
|
|
const uid = useUserId();
|
|
|
|
const googleAnalyticsEnabled = useSetting('GoogleAnalytics_enabled', false);
|
|
const googleId = useSetting('GoogleAnalytics_ID', '');
|
|
|
|
const piwiEnabled = useSetting('PiwikAnalytics_enabled', false);
|
|
const piwikUrl = useSetting('PiwikAnalytics_url', '');
|
|
|
|
const piwikSiteId = useSetting('PiwikAnalytics_siteId', '');
|
|
const piwikPrependDomain = useSetting('PiwikAnalytics_prependDomain', '');
|
|
const piwikCookieDomain = useSetting('PiwikAnalytics_cookieDomain', '');
|
|
const piwikDomains = useSetting('PiwikAnalytics_domains', '');
|
|
const piwikAdditionalTracker = useSetting('PiwikAdditionalTrackers', '');
|
|
|
|
useEffect(() => {
|
|
if (uid) {
|
|
window._paq = window._paq || [];
|
|
window._paq.push(['setUserId', uid]);
|
|
}
|
|
}, [uid]);
|
|
|
|
useEffect(() => {
|
|
if (!googleAnalyticsEnabled || !googleId) {
|
|
return;
|
|
}
|
|
if (googleId.startsWith('G-')) {
|
|
// Google Analytics 4
|
|
const f = document.getElementsByTagName('script')[0];
|
|
const j = document.createElement('script');
|
|
j.async = true;
|
|
j.src = `//www.googletagmanager.com/gtag/js?id=${googleId}`;
|
|
f.parentNode?.insertBefore(j, f);
|
|
|
|
// injecting the dataLayer into the windows global object
|
|
const w: Window & { dataLayer?: any } = window;
|
|
const dataLayer = w.dataLayer || [];
|
|
const gtag = (key: string, value: any) => {
|
|
dataLayer.push(key, value);
|
|
};
|
|
gtag('js', new Date());
|
|
gtag('config', googleId);
|
|
} else {
|
|
// Google Analytics 3
|
|
(function (i, s, o, g, r: 'ga', a?: any, m?: any) {
|
|
i.GoogleAnalyticsObject = r;
|
|
((i[r] =
|
|
i[r] ||
|
|
function (...args) {
|
|
((i[r] as any).q = (i[r] as any).q || []).push(args);
|
|
}),
|
|
((i[r] as any).l = new Date().getTime()));
|
|
|
|
((a = s.createElement(o)), (m = s.getElementsByTagName(o)[0]));
|
|
a.async = 1;
|
|
a.src = g;
|
|
m.parentNode.insertBefore(a, m);
|
|
})(window, document, 'script', 'https://www.google-analytics.com/analytics.js', 'ga');
|
|
|
|
window.ga?.('create', googleId, 'auto');
|
|
window.ga?.('send', 'pageview');
|
|
}
|
|
}, [googleAnalyticsEnabled, googleId, uid]);
|
|
|
|
useEffect(() => {
|
|
if (!piwiEnabled || !piwikUrl) {
|
|
document.getElementById('piwik-analytics')?.remove();
|
|
window._paq = [];
|
|
return;
|
|
}
|
|
|
|
window._paq = window._paq || [];
|
|
|
|
window._paq.push(['trackPageView']);
|
|
window._paq.push(['enableLinkTracking']);
|
|
if (piwikPrependDomain) {
|
|
window._paq.push(['setDocumentTitle', `${window.location.hostname}/${document.title}`]);
|
|
}
|
|
const upperLevelDomain = `*.${window.location.hostname.split('.').slice(1).join('.')}`;
|
|
if (piwikCookieDomain) {
|
|
window._paq.push(['setCookieDomain', upperLevelDomain]);
|
|
}
|
|
if (piwikDomains) {
|
|
// array
|
|
const domainsArray = piwikDomains.split(/\n/);
|
|
const domains = [];
|
|
for (let i = 0; i < domainsArray.length; i++) {
|
|
// only push domain if it contains a non whitespace character.
|
|
if (/\S/.test(domainsArray[i])) {
|
|
domains.push(`*.${domainsArray[i].trim()}`);
|
|
}
|
|
}
|
|
window._paq.push(['setDomains', domains]);
|
|
}
|
|
(() => {
|
|
try {
|
|
if (/\S/.test(piwikAdditionalTracker)) {
|
|
// piwikAdditionalTracker is not empty or whitespace only
|
|
const addTrackers = JSON.parse(piwikAdditionalTracker);
|
|
for (let i = 0; i < addTrackers.length; i++) {
|
|
const tracker = addTrackers[i];
|
|
window._paq.push(['addTracker', `${tracker.trackerURL}js/`, tracker.siteId]);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
// parsing JSON faild
|
|
console.log('Error while parsing JSON value of "piwikAdditionalTracker": ', e);
|
|
}
|
|
window._paq.push(['setTrackerUrl', `${piwikUrl}js/`]);
|
|
window._paq.push(['setSiteId', Number.parseInt(piwikSiteId)]);
|
|
const d = document;
|
|
const g = d.createElement('script');
|
|
g.setAttribute('id', 'piwik-analytics');
|
|
const s = d.getElementsByTagName('script')[0];
|
|
g.type = 'text/javascript';
|
|
g.async = true;
|
|
g.defer = true;
|
|
g.src = `${piwikUrl}js/`;
|
|
s.parentNode?.insertBefore(g, s);
|
|
})();
|
|
}, [piwiEnabled, piwikAdditionalTracker, piwikCookieDomain, piwikDomains, piwikPrependDomain, piwikSiteId, piwikUrl]);
|
|
};
|