feat: Move Google Tag Manager ID to env variable

This commit is contained in:
oSumAtrIX 2024-11-30 05:13:13 +01:00
parent 504a670e4e
commit 586c67a9ca
No known key found for this signature in database
GPG Key ID: A9B3094ACDB604B4
6 changed files with 42 additions and 45 deletions

1
.env
View File

@ -1 +0,0 @@
RV_API_URL="https://api.revanced.app"

2
.env.example Normal file
View File

@ -0,0 +1,2 @@
RV_API_URL=https://api.revanced.app
RV_GOOGLE_TAG_MANAGER_ID=

2
.gitignore vendored
View File

@ -4,6 +4,6 @@ node_modules
/public /public
/.svelte-kit /.svelte-kit
/package /package
!.env.example .env
/_docs_src /_docs_src
/static/docs /static/docs

View File

@ -1,17 +1,17 @@
import { browser } from '$app/environment'; import { browser } from '$app/environment';
import { RV_API_URL } from '$env/static/public'; import { RV_API_URL } from '$env/static/public';
const URL_KEY = 'revanced_api_url'; export const default_api_url = RV_API_URL;
export const default_base_url = RV_API_URL; const URL_KEY = 'revanced_api_url';
// Get base URL // Get base URL
export function api_base_url(): string { export function api_base_url(): string {
if (browser) { if (browser) {
return localStorage.getItem(URL_KEY) || default_base_url; return localStorage.getItem(URL_KEY) || default_api_url;
} }
return default_base_url; return default_api_url;
} }
// (re)set base URL. // (re)set base URL.

View File

@ -9,7 +9,7 @@
import Modal from '$lib/components/Dialogue.svelte'; import Modal from '$lib/components/Dialogue.svelte';
import Button from '$lib/components/Button.svelte'; import Button from '$lib/components/Button.svelte';
import * as settings from '$data/api/settings'; import { api_base_url, set_api_base_url, default_api_url } from '$data/api/settings';
import RouterEvents from '$data/RouterEvents'; import RouterEvents from '$data/RouterEvents';
import { useQueryClient } from '@tanstack/svelte-query'; import { useQueryClient } from '@tanstack/svelte-query';
@ -28,15 +28,15 @@
reload(); reload();
} }
let url = settings.api_base_url(); let url = api_base_url();
function save() { function save() {
settings.set_api_base_url(url); set_api_base_url(url);
reload(); reload();
} }
function reset() { function reset() {
url = settings.default_base_url; url = default_api_url;
} }
let menuOpen = false; let menuOpen = false;

View File

@ -24,6 +24,8 @@
import RouterEvents from '$data/RouterEvents'; import RouterEvents from '$data/RouterEvents';
import { events as themeEvents } from '$util/themeEvents'; import { events as themeEvents } from '$util/themeEvents';
import { RV_GOOGLE_TAG_MANAGER_ID } from '$env/static/public';
const queryClient = new QueryClient({ const queryClient = new QueryClient({
defaultOptions: { defaultOptions: {
queries: { queries: {
@ -36,24 +38,38 @@
let showConsentModal = false; let showConsentModal = false;
let allowAnalytics = false; let allowAnalytics = false;
function enableAnalytics() {
window.dataLayer = window.dataLayer || [];
function gtag() {
dataLayer.push(arguments);
}
gtag('js', new Date());
gtag('config', RV_GOOGLE_TAG_MANAGER_ID);
var s = document.createElement('script');
s.src = `https://www.googletagmanager.com/gtm.js?id=${RV_GOOGLE_TAG_MANAGER_ID}`;
document.head.append(s);
}
function rememberChoice(allow: boolean) { function rememberChoice(allow: boolean) {
localStorage.setItem('analytics', allow.toString()); localStorage.setItem('analytics', allow.toString());
showConsentModal = false; showConsentModal = false;
allowAnalytics = allow; allowAnalytics = allow;
if (allowAnalytics) enableAnalytics();
} }
onMount(() => { onMount(() => {
new DateTriggerEventHandler(themeEvents);
// Check if the user has already decided.
// Check if the user has already decided // Check if the user has already decided
const hasDecided = localStorage.getItem('analytics') !== null; const hasDecided = localStorage.getItem('analytics') !== null;
if (hasDecided) { if (hasDecided) {
allowAnalytics = localStorage.getItem('analytics') === 'true'; allowAnalytics = localStorage.getItem('analytics') === 'true';
if (allowAnalytics) enableAnalytics();
} else { } else {
showConsentModal = true; showConsentModal = true;
} }
new DateTriggerEventHandler(themeEvents);
isRestoring.set(true); isRestoring.set(true);
const [unsubscribe, promise] = persistQueryClient({ const [unsubscribe, promise] = persistQueryClient({
queryClient, queryClient,
@ -79,26 +95,18 @@
); );
</script> </script>
<svelte:head> {#if allowAnalytics}
{#if allowAnalytics} <!-- Google Tag Manager (noscript) -->
<!-- Google Tag Manager --> <noscript>
<script> <!-- svelte-ignore a11y-missing-attribute -->
allowAnalytics = localStorage.getItem('analytics') === 'true'; <iframe
if (allowAnalytics) { src="https://www.googletagmanager.com/ns.html?id={RV_GOOGLE_TAG_MANAGER_ID}"
(function (w, d, s, l, i) { height="0"
w[l] = w[l] || []; width="0"
w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' }); style="display: none; visibility: hidden"
var f = d.getElementsByTagName(s)[0], ></iframe>
j = d.createElement(s), </noscript>
dl = l != 'dataLayer' ? '&l=' + l : ''; {/if}
j.async = true;
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
f.parentNode.insertBefore(j, f);
})(window, document, 'script', 'dataLayer', 'GTM-MQ6K849');
}
</script>
{/if}
</svelte:head>
<QueryClientProvider client={queryClient}> <QueryClientProvider client={queryClient}>
<NavHost /> <NavHost />
@ -123,15 +131,3 @@
</div> </div>
<!-- <Footer> --> <!-- <Footer> -->
</QueryClientProvider> </QueryClientProvider>
{#if allowAnalytics}
<!-- Google Tag Manager (noscript) -->
<noscript>
<!-- svelte-ignore a11y-missing-attribute -->
<iframe
src="https://www.googletagmanager.com/ns.html?id=GTM-MQ6K849"
height="0"
width="0"
style="display: none; visibility: hidden"
></iframe>
</noscript>
{/if}