Skip to content

Commit 3bb3812

Browse files
authored
fix: gtm debug mode (#257)
1 parent 0671641 commit 3bb3812

File tree

1 file changed

+29
-4
lines changed

1 file changed

+29
-4
lines changed

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

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import {
88
makeEnvironmentProviders,
99
PLATFORM_ID,
1010
} from '@angular/core';
11+
import { NavigationStart, Router } from '@angular/router';
12+
import { filter, first, tap } from 'rxjs';
1113

1214
/**
1315
* Credits https://github.com/find-ida/angular-ssr-partytown
@@ -77,19 +79,42 @@ export type PartyTownPixelFactory = (img: HTMLImageElement) => HTMLImageElement;
7779
export class PartyTownService {
7880
private readonly _config = inject(PARTY_TOWN_CONFIG, { optional: true });
7981
private readonly _document = inject(DOCUMENT);
82+
private readonly _router = inject(Router);
8083

8184
init(): void {
8285
if (this._config?.partyTown?.enabled) {
83-
this.initPartyTownScript();
84-
this.initScripts(...(this._config.scripts ?? []));
85-
this.initPixels(...(this._config.pixels ?? []));
86+
this._router.events
87+
.pipe(
88+
filter((ev): ev is NavigationStart => ev instanceof NavigationStart),
89+
tap({
90+
next: (asd) => {
91+
this.initPartyTownScript();
92+
const disablePartyTown = asd.url.includes('gtm_debug=');
93+
94+
this.initScripts(
95+
disablePartyTown,
96+
...(this._config?.scripts ?? []),
97+
);
98+
this.initPixels(...(this._config?.pixels ?? []));
99+
},
100+
}),
101+
first(),
102+
)
103+
.subscribe();
86104
}
87105
}
88106

89-
private initScripts(...scripts: PartyTownScriptFactory[]): void {
107+
private initScripts(
108+
disablePartyTown = false,
109+
...scripts: PartyTownScriptFactory[]
110+
): void {
90111
scripts.forEach((script) => {
91112
const scriptElement = this._document.createElement('script');
92113
const _script = script(scriptElement);
114+
if (disablePartyTown) {
115+
_script.removeAttribute('data-type');
116+
}
117+
93118
this._document.head.appendChild(_script);
94119
});
95120
}

0 commit comments

Comments
 (0)