Skip to content

Commit 763bb03

Browse files
authored
refactor: disable partytown (#269)
1 parent de61818 commit 763bb03

File tree

13 files changed

+128
-125
lines changed

13 files changed

+128
-125
lines changed

apps/blog/project.json

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@
2525
"assets": [
2626
"apps/blog/src/assets",
2727
"apps/blog/src/sitemap.xml",
28-
"apps/blog/src/_redirects",
29-
{
30-
"glob": "**/*",
31-
"input": "node_modules/@builder.io/partytown/lib",
32-
"output": "/~partytown"
33-
}
28+
"apps/blog/src/_redirects"
3429
],
3530
"styles": [
3631
"apps/blog/src/styles.scss",
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { EnvironmentProviders, makeEnvironmentProviders } from '@angular/core';
22

33
import {
4-
consent,
5-
gtag,
4+
consentUpdateScript,
65
gtmScript,
6+
initialConsentScript,
7+
metaScript,
78
provideTracking,
89
} from '@angular-love/blog/tracking/feature';
910

@@ -13,34 +14,33 @@ export const provideAppTracking = (): EnvironmentProviders => {
1314
return makeEnvironmentProviders([
1415
provideTracking({
1516
partyTown: {
16-
partyTown: {
17-
forward: [
18-
['dataLayer.push', { preserveBehavior: true }],
19-
['fbq', { preserveBehavior: true }],
20-
],
21-
enabled: true,
22-
reverseProxy: 'https://reverse.contact-ef8.workers.dev/',
23-
proxiedHosts: [
24-
'region1.analytics.google.com',
25-
'www.google-analytics.com',
26-
'www.googletagmanager.com',
27-
'googletagmanager.com',
28-
'connect.facebook.net',
29-
'googleads.g.doubleclick.net',
30-
'snap.licdn.com',
31-
'static.ads-twitter.com',
32-
],
33-
},
34-
scripts: [
35-
gtag(),
36-
gtmScript('GTM-5XNT5NS'),
37-
consent('ads', 'ad_storage', 'granted'),
38-
consent('ads', 'ad_storage', 'denied'),
39-
consent('analytics', 'analytics_storage', 'granted'),
40-
consent('analytics', 'analytics_storage', 'denied'),
17+
forward: [
18+
['dataLayer.push', { preserveBehavior: true }],
19+
['fbq', { preserveBehavior: true }],
20+
],
21+
enabled: false,
22+
reverseProxy: 'https://reverse.contact-ef8.workers.dev/',
23+
proxiedHosts: [
24+
'region1.analytics.google.com',
25+
'www.google-analytics.com',
26+
'www.googletagmanager.com',
27+
'googletagmanager.com',
28+
'connect.facebook.net',
29+
'googleads.g.doubleclick.net',
30+
'snap.licdn.com',
31+
'static.ads-twitter.com',
4132
],
4233
},
4334
cookieConsent: cookieConsentConfig,
35+
scripts: [
36+
initialConsentScript(),
37+
gtmScript('GTM-5XNT5NS'),
38+
consentUpdateScript('ads', 'ad_storage', 'granted'),
39+
consentUpdateScript('ads', 'ad_storage', 'denied'),
40+
consentUpdateScript('analytics', 'analytics_storage', 'granted'),
41+
consentUpdateScript('analytics', 'analytics_storage', 'denied'),
42+
metaScript('284876369340184'),
43+
],
4444
}),
4545
]);
4646
};

apps/blog/src/environments/environment.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { provideConfig } from '@angular-love/shared/config';
22

33
import { AppEnvironment } from './app-environment';
4-
import { provideAppTracking } from "../app/providers/tracking";
54

65
export const environment: AppEnvironment = {
76
baseUrl: process.env.AL_BASE_URL,
@@ -17,6 +16,5 @@ export const environment: AppEnvironment = {
1716
giscusCategory: process.env.AL_GISCUS_CATEGORY,
1817
giscusCategoryId: process.env.AL_GISCUS_CATEGORY_ID,
1918
}),
20-
provideAppTracking(),
2119
],
2220
};
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
export * from './lib/tracking';
2-
export * from './lib/partytown/gtm-script';
3-
export * from './lib/partytown/meta-script';
4-
export * from './lib/partytown/meta-pixel';
2+
export * from './lib/scripts';
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
export * from './gtm-script';
2-
export * from './meta-script';
31
export * from './partytown.service';
4-
export * from './meta-pixel';

libs/blog/tracking/feature/src/lib/partytown/meta-pixel.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

libs/blog/tracking/feature/src/lib/partytown/partytown.service.ts

Lines changed: 21 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,18 @@ import {
99
PLATFORM_ID,
1010
} from '@angular/core';
1111

12+
import { ScriptFactory, ScriptsLoader } from '../scripts-loader';
13+
1214
/**
1315
* Credits https://github.com/find-ida/angular-ssr-partytown
1416
*/
1517
export type PartyTownConfig = {
16-
partyTown?: {
17-
enabled: boolean;
18-
debug?: boolean;
19-
basePath?: string;
20-
forward?: (string | [string, { preserveBehavior: boolean }])[];
21-
reverseProxy?: string;
22-
proxiedHosts?: string[];
23-
};
24-
scripts?: PartyTownScriptFactory[];
25-
pixels?: PartyTownPixelFactory[];
18+
enabled: boolean;
19+
debug?: boolean;
20+
basePath?: string;
21+
forward?: (string | [string, { preserveBehavior: boolean }])[];
22+
reverseProxy?: string;
23+
proxiedHosts?: string[];
2624
};
2725

2826
export const PARTY_TOWN_CONFIG = new InjectionToken<PartyTownConfig>(
@@ -37,60 +35,34 @@ export const providePartyTown = (
3735
provide: PARTY_TOWN_CONFIG,
3836
useValue: {
3937
...config,
40-
partyTown: {
41-
...config.partyTown,
42-
enabled: config.partyTown?.enabled ?? false,
43-
debug: config.partyTown?.debug ?? false,
44-
basePath: config.partyTown?.basePath ?? '/~partytown',
45-
forward: config.partyTown?.forward ?? [],
46-
reverseProxy: config.partyTown?.reverseProxy ?? '',
47-
proxiedHosts: config.partyTown?.proxiedHosts ?? [],
48-
},
49-
scripts: config?.scripts ?? [],
50-
pixels: config?.pixels ?? [],
38+
enabled: config?.enabled ?? false,
39+
debug: config?.debug ?? false,
40+
basePath: config?.basePath ?? '/~partytown',
41+
forward: config?.forward ?? [],
42+
reverseProxy: config?.reverseProxy ?? '',
43+
proxiedHosts: config?.proxiedHosts ?? [],
5144
} satisfies PartyTownConfig,
5245
},
5346
PartyTownService,
54-
{
55-
provide: APP_INITIALIZER,
56-
multi: true,
57-
useFactory: () => {
58-
const platformId = inject(PLATFORM_ID);
59-
const partyTownService = inject(PartyTownService);
60-
61-
return () => {
62-
if (config.partyTown?.enabled && isPlatformBrowser(platformId)) {
63-
partyTownService.init();
64-
}
65-
};
66-
},
67-
},
6847
]);
6948

70-
export type PartyTownScriptFactory = (
71-
scriptElement: HTMLScriptElement,
72-
) => HTMLScriptElement;
73-
74-
export type PartyTownPixelFactory = (img: HTMLImageElement) => HTMLImageElement;
75-
7649
@Injectable()
77-
export class PartyTownService {
50+
export class PartyTownService implements ScriptsLoader {
7851
private readonly _config = inject(PARTY_TOWN_CONFIG, { optional: true });
7952
private readonly _document = inject(DOCUMENT);
8053

81-
init(): void {
82-
if (this._config?.partyTown?.enabled) {
54+
init(scripts: ScriptFactory[]): void {
55+
if (this._config?.enabled) {
8356
this.initPartyTownScript();
8457
const disablePartyTown = window.location.search.includes('gtm_debug=');
8558

86-
this.initScripts(disablePartyTown, ...(this._config?.scripts ?? []));
87-
this.initPixels(...(this._config?.pixels ?? []));
59+
this.initScripts(disablePartyTown, ...(scripts ?? []));
8860
}
8961
}
9062

9163
private initScripts(
9264
disablePartyTown = false,
93-
...scripts: PartyTownScriptFactory[]
65+
...scripts: ScriptFactory[]
9466
): void {
9567
scripts.forEach((script) => {
9668
const scriptElement = this._document.createElement('script');
@@ -108,19 +80,9 @@ export class PartyTownService {
10880
});
10981
}
11082

111-
private initPixels(...scripts: PartyTownPixelFactory[]): void {
112-
scripts.forEach((pixel) => {
113-
const noScriptElement = this._document.createElement('noscript');
114-
const pixelElement = this._document.createElement('img');
115-
const _pixel = pixel(pixelElement);
116-
noScriptElement.append(_pixel);
117-
this._document.head.appendChild(noScriptElement);
118-
});
119-
}
120-
12183
private initPartyTownScript(): void {
122-
if (!this._config?.partyTown) return;
123-
const config = this._config.partyTown;
84+
if (!this._config) return;
85+
const config = this._config;
12486

12587
// Config Script
12688
const partyTownConfigurationScript = this._document.createElement('script');
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { DOCUMENT } from '@angular/common';
2+
import { inject, Injectable } from '@angular/core';
3+
4+
import { ScriptFactory, ScriptsLoader } from './scripts-loader';
5+
6+
@Injectable()
7+
export class ScriptsLoaderService implements ScriptsLoader {
8+
private readonly _document = inject(DOCUMENT);
9+
10+
init(scripts: ScriptFactory[]): void {
11+
scripts.forEach((script) => {
12+
const scriptElement = this._document.createElement('script');
13+
const _script = script(scriptElement);
14+
this._document.head.appendChild(_script);
15+
});
16+
}
17+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export type ScriptFactory = (
2+
scriptElement: HTMLScriptElement,
3+
) => HTMLScriptElement;
4+
5+
export abstract class ScriptsLoader {
6+
abstract init(scripts: ScriptFactory[]): void;
7+
}

libs/blog/tracking/feature/src/lib/partytown/gtm-script.ts renamed to libs/blog/tracking/feature/src/lib/scripts/gtm-script.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { PartyTownScriptFactory } from './partytown.service';
1+
import { ScriptFactory } from '../scripts-loader';
22

3-
export const gtag = (): PartyTownScriptFactory => (gtmScript) => {
3+
export const initialConsentScript = (): ScriptFactory => (gtmScript) => {
44
gtmScript.textContent = `
55
window.dataLayer = window.dataLayer || [];
66
window.gtag = function gtag(){dataLayer.push(arguments);}
@@ -14,7 +14,7 @@ export const gtag = (): PartyTownScriptFactory => (gtmScript) => {
1414
};
1515

1616
export const gtmScript =
17-
(id: string): PartyTownScriptFactory =>
17+
(id: string): ScriptFactory =>
1818
(gtmScript) => {
1919
gtmScript.textContent = `(function (w, d, s, l, i) {
2020
w[l] = w[l] || [];
@@ -27,12 +27,12 @@ export const gtmScript =
2727
return gtmScript;
2828
};
2929

30-
export const consent =
30+
export const consentUpdateScript =
3131
(
3232
category: 'analytics' | 'ads',
3333
consentType: 'ad_storage' | 'analytics_storage',
3434
status: 'granted' | 'denied',
35-
): PartyTownScriptFactory =>
35+
): ScriptFactory =>
3636
(gtmScript) => {
3737
gtmScript.setAttribute('type', 'text/plain');
3838
gtmScript.setAttribute(

0 commit comments

Comments
 (0)