From 586c67a9ca47f78832023857cf5ae81f6f4fdf0f Mon Sep 17 00:00:00 2001 From: oSumAtrIX Date: Sat, 30 Nov 2024 05:13:13 +0100 Subject: [PATCH] feat: Move Google Tag Manager ID to env variable --- .env | 1 - .env.example | 2 + .gitignore | 2 +- src/data/api/settings.ts | 8 ++-- src/layout/Navbar/NavHost.svelte | 8 ++-- src/routes/+layout.svelte | 66 +++++++++++++++----------------- 6 files changed, 42 insertions(+), 45 deletions(-) delete mode 100644 .env create mode 100644 .env.example diff --git a/.env b/.env deleted file mode 100644 index 7669f5d7..00000000 --- a/.env +++ /dev/null @@ -1 +0,0 @@ -RV_API_URL="https://api.revanced.app" diff --git a/.env.example b/.env.example new file mode 100644 index 00000000..ed851b93 --- /dev/null +++ b/.env.example @@ -0,0 +1,2 @@ +RV_API_URL=https://api.revanced.app +RV_GOOGLE_TAG_MANAGER_ID= \ No newline at end of file diff --git a/.gitignore b/.gitignore index 4084ab48..81d9cb74 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,6 @@ node_modules /public /.svelte-kit /package -!.env.example +.env /_docs_src /static/docs diff --git a/src/data/api/settings.ts b/src/data/api/settings.ts index 1e50660a..1b571a2a 100644 --- a/src/data/api/settings.ts +++ b/src/data/api/settings.ts @@ -1,17 +1,17 @@ import { browser } from '$app/environment'; 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 export function api_base_url(): string { 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. diff --git a/src/layout/Navbar/NavHost.svelte b/src/layout/Navbar/NavHost.svelte index 5338e593..705a7858 100644 --- a/src/layout/Navbar/NavHost.svelte +++ b/src/layout/Navbar/NavHost.svelte @@ -9,7 +9,7 @@ import Modal from '$lib/components/Dialogue.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 { useQueryClient } from '@tanstack/svelte-query'; @@ -28,15 +28,15 @@ reload(); } - let url = settings.api_base_url(); + let url = api_base_url(); function save() { - settings.set_api_base_url(url); + set_api_base_url(url); reload(); } function reset() { - url = settings.default_base_url; + url = default_api_url; } let menuOpen = false; diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index 4078cf4d..7db54c8d 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -24,6 +24,8 @@ import RouterEvents from '$data/RouterEvents'; import { events as themeEvents } from '$util/themeEvents'; + import { RV_GOOGLE_TAG_MANAGER_ID } from '$env/static/public'; + const queryClient = new QueryClient({ defaultOptions: { queries: { @@ -36,24 +38,38 @@ let showConsentModal = 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) { localStorage.setItem('analytics', allow.toString()); showConsentModal = false; allowAnalytics = allow; + + if (allowAnalytics) enableAnalytics(); } onMount(() => { - new DateTriggerEventHandler(themeEvents); - - // Check if the user has already decided. // Check if the user has already decided const hasDecided = localStorage.getItem('analytics') !== null; if (hasDecided) { allowAnalytics = localStorage.getItem('analytics') === 'true'; + if (allowAnalytics) enableAnalytics(); } else { showConsentModal = true; } + new DateTriggerEventHandler(themeEvents); + isRestoring.set(true); const [unsubscribe, promise] = persistQueryClient({ queryClient, @@ -79,26 +95,18 @@ ); - - {#if allowAnalytics} - - - {/if} - +{#if allowAnalytics} + + +{/if} @@ -123,15 +131,3 @@ -{#if allowAnalytics} - - -{/if}