Skip to content

Commit 586c67a

Browse files
committed
feat: Move Google Tag Manager ID to env variable
1 parent 504a670 commit 586c67a

File tree

6 files changed

+42
-45
lines changed

6 files changed

+42
-45
lines changed

.env

Lines changed: 0 additions & 1 deletion
This file was deleted.

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
RV_API_URL=https://api.revanced.app
2+
RV_GOOGLE_TAG_MANAGER_ID=

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ node_modules
44
/public
55
/.svelte-kit
66
/package
7-
!.env.example
7+
.env
88
/_docs_src
99
/static/docs

src/data/api/settings.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { browser } from '$app/environment';
22
import { RV_API_URL } from '$env/static/public';
33

4-
const URL_KEY = 'revanced_api_url';
4+
export const default_api_url = RV_API_URL;
55

6-
export const default_base_url = RV_API_URL;
6+
const URL_KEY = 'revanced_api_url';
77

88
// Get base URL
99
export function api_base_url(): string {
1010
if (browser) {
11-
return localStorage.getItem(URL_KEY) || default_base_url;
11+
return localStorage.getItem(URL_KEY) || default_api_url;
1212
}
1313

14-
return default_base_url;
14+
return default_api_url;
1515
}
1616

1717
// (re)set base URL.

src/layout/Navbar/NavHost.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Modal from '$lib/components/Dialogue.svelte';
1010
import Button from '$lib/components/Button.svelte';
1111
12-
import * as settings from '$data/api/settings';
12+
import { api_base_url, set_api_base_url, default_api_url } from '$data/api/settings';
1313
import RouterEvents from '$data/RouterEvents';
1414
1515
import { useQueryClient } from '@tanstack/svelte-query';
@@ -28,15 +28,15 @@
2828
reload();
2929
}
3030
31-
let url = settings.api_base_url();
31+
let url = api_base_url();
3232
3333
function save() {
34-
settings.set_api_base_url(url);
34+
set_api_base_url(url);
3535
reload();
3636
}
3737
3838
function reset() {
39-
url = settings.default_base_url;
39+
url = default_api_url;
4040
}
4141
4242
let menuOpen = false;

src/routes/+layout.svelte

Lines changed: 31 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import RouterEvents from '$data/RouterEvents';
2525
import { events as themeEvents } from '$util/themeEvents';
2626
27+
import { RV_GOOGLE_TAG_MANAGER_ID } from '$env/static/public';
28+
2729
const queryClient = new QueryClient({
2830
defaultOptions: {
2931
queries: {
@@ -36,24 +38,38 @@
3638
let showConsentModal = false;
3739
let allowAnalytics = false;
3840
41+
function enableAnalytics() {
42+
window.dataLayer = window.dataLayer || [];
43+
function gtag() {
44+
dataLayer.push(arguments);
45+
}
46+
gtag('js', new Date());
47+
gtag('config', RV_GOOGLE_TAG_MANAGER_ID);
48+
var s = document.createElement('script');
49+
s.src = `https://www.googletagmanager.com/gtm.js?id=${RV_GOOGLE_TAG_MANAGER_ID}`;
50+
document.head.append(s);
51+
}
52+
3953
function rememberChoice(allow: boolean) {
4054
localStorage.setItem('analytics', allow.toString());
4155
showConsentModal = false;
4256
allowAnalytics = allow;
57+
58+
if (allowAnalytics) enableAnalytics();
4359
}
4460
4561
onMount(() => {
46-
new DateTriggerEventHandler(themeEvents);
47-
48-
// Check if the user has already decided.
4962
// Check if the user has already decided
5063
const hasDecided = localStorage.getItem('analytics') !== null;
5164
if (hasDecided) {
5265
allowAnalytics = localStorage.getItem('analytics') === 'true';
66+
if (allowAnalytics) enableAnalytics();
5367
} else {
5468
showConsentModal = true;
5569
}
5670
71+
new DateTriggerEventHandler(themeEvents);
72+
5773
isRestoring.set(true);
5874
const [unsubscribe, promise] = persistQueryClient({
5975
queryClient,
@@ -79,26 +95,18 @@
7995
);
8096
</script>
8197

82-
<svelte:head>
83-
{#if allowAnalytics}
84-
<!-- Google Tag Manager -->
85-
<script>
86-
allowAnalytics = localStorage.getItem('analytics') === 'true';
87-
if (allowAnalytics) {
88-
(function (w, d, s, l, i) {
89-
w[l] = w[l] || [];
90-
w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
91-
var f = d.getElementsByTagName(s)[0],
92-
j = d.createElement(s),
93-
dl = l != 'dataLayer' ? '&l=' + l : '';
94-
j.async = true;
95-
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
96-
f.parentNode.insertBefore(j, f);
97-
})(window, document, 'script', 'dataLayer', 'GTM-MQ6K849');
98-
}
99-
</script>
100-
{/if}
101-
</svelte:head>
98+
{#if allowAnalytics}
99+
<!-- Google Tag Manager (noscript) -->
100+
<noscript>
101+
<!-- svelte-ignore a11y-missing-attribute -->
102+
<iframe
103+
src="https://www.googletagmanager.com/ns.html?id={RV_GOOGLE_TAG_MANAGER_ID}"
104+
height="0"
105+
width="0"
106+
style="display: none; visibility: hidden"
107+
></iframe>
108+
</noscript>
109+
{/if}
102110

103111
<QueryClientProvider client={queryClient}>
104112
<NavHost />
@@ -123,15 +131,3 @@
123131
</div>
124132
<!-- <Footer> -->
125133
</QueryClientProvider>
126-
{#if allowAnalytics}
127-
<!-- Google Tag Manager (noscript) -->
128-
<noscript>
129-
<!-- svelte-ignore a11y-missing-attribute -->
130-
<iframe
131-
src="https://www.googletagmanager.com/ns.html?id=GTM-MQ6K849"
132-
height="0"
133-
width="0"
134-
style="display: none; visibility: hidden"
135-
></iframe>
136-
</noscript>
137-
{/if}

0 commit comments

Comments
 (0)