|
8 | 8 | makeEnvironmentProviders,
|
9 | 9 | PLATFORM_ID,
|
10 | 10 | } from '@angular/core';
|
| 11 | +import { NavigationStart, Router } from '@angular/router'; |
| 12 | +import { filter, first, tap } from 'rxjs'; |
11 | 13 |
|
12 | 14 | /**
|
13 | 15 | * Credits https://github.com/find-ida/angular-ssr-partytown
|
@@ -77,19 +79,42 @@ export type PartyTownPixelFactory = (img: HTMLImageElement) => HTMLImageElement;
|
77 | 79 | export class PartyTownService {
|
78 | 80 | private readonly _config = inject(PARTY_TOWN_CONFIG, { optional: true });
|
79 | 81 | private readonly _document = inject(DOCUMENT);
|
| 82 | + private readonly _router = inject(Router); |
80 | 83 |
|
81 | 84 | init(): void {
|
82 | 85 | 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(); |
86 | 104 | }
|
87 | 105 | }
|
88 | 106 |
|
89 |
| - private initScripts(...scripts: PartyTownScriptFactory[]): void { |
| 107 | + private initScripts( |
| 108 | + disablePartyTown = false, |
| 109 | + ...scripts: PartyTownScriptFactory[] |
| 110 | + ): void { |
90 | 111 | scripts.forEach((script) => {
|
91 | 112 | const scriptElement = this._document.createElement('script');
|
92 | 113 | const _script = script(scriptElement);
|
| 114 | + if (disablePartyTown) { |
| 115 | + _script.removeAttribute('data-type'); |
| 116 | + } |
| 117 | + |
93 | 118 | this._document.head.appendChild(_script);
|
94 | 119 | });
|
95 | 120 | }
|
|
0 commit comments