Skip to content

Commit f4bb389

Browse files
authored
fix: meta pixel (#270)
1 parent 763bb03 commit f4bb389

File tree

10 files changed

+54
-11
lines changed

10 files changed

+54
-11
lines changed

apps/blog/src/app/providers/tracking/cookie-consent.config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ export const cookieConsentConfig = {
2424
},
2525
ads: {},
2626
},
27+
cookie: {
28+
name: 'cc_cookie_al',
29+
},
2730
onConsent: () => {
2831
window.dataLayer.push({ event: 'consent_update' });
2932
window.dispatchEvent(new CustomEvent('ptupdate'));

apps/blog/src/app/providers/tracking/tracking.provider.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
consentUpdateScript,
55
gtmScript,
66
initialConsentScript,
7+
metaPixel,
78
metaScript,
89
provideTracking,
910
} from '@angular-love/blog/tracking/feature';
@@ -41,6 +42,7 @@ export const provideAppTracking = (): EnvironmentProviders => {
4142
consentUpdateScript('analytics', 'analytics_storage', 'denied'),
4243
metaScript('284876369340184'),
4344
],
45+
pixels: [metaPixel('284876369340184')],
4446
}),
4547
]);
4648
};

apps/blog/src/environments/environment.ts

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

3+
import { provideAppTracking } from '../app/providers/tracking';
4+
35
import { AppEnvironment } from './app-environment';
46

57
export const environment: AppEnvironment = {
@@ -16,5 +18,6 @@ export const environment: AppEnvironment = {
1618
giscusCategory: process.env.AL_GISCUS_CATEGORY,
1719
giscusCategoryId: process.env.AL_GISCUS_CATEGORY_ID,
1820
}),
21+
provideAppTracking(),
1922
],
2023
};

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { DOCUMENT, isPlatformBrowser } from '@angular/common';
1+
import { DOCUMENT } from '@angular/common';
22
import {
3-
APP_INITIALIZER,
43
EnvironmentProviders,
54
inject,
65
Injectable,
76
InjectionToken,
87
makeEnvironmentProviders,
9-
PLATFORM_ID,
108
} from '@angular/core';
119

12-
import { ScriptFactory, ScriptsLoader } from '../scripts-loader';
10+
import { PixelFactory, ScriptFactory, ScriptsLoader } from '../scripts-loader';
1311

1412
/**
1513
* Credits https://github.com/find-ida/angular-ssr-partytown
@@ -51,12 +49,13 @@ export class PartyTownService implements ScriptsLoader {
5149
private readonly _config = inject(PARTY_TOWN_CONFIG, { optional: true });
5250
private readonly _document = inject(DOCUMENT);
5351

54-
init(scripts: ScriptFactory[]): void {
52+
init(scripts: ScriptFactory[], pixels: PixelFactory[]): void {
5553
if (this._config?.enabled) {
5654
this.initPartyTownScript();
5755
const disablePartyTown = window.location.search.includes('gtm_debug=');
5856

5957
this.initScripts(disablePartyTown, ...(scripts ?? []));
58+
this.initPixels(pixels);
6059
}
6160
}
6261

@@ -80,6 +79,16 @@ export class PartyTownService implements ScriptsLoader {
8079
});
8180
}
8281

82+
private initPixels(pixels: PixelFactory[]): void {
83+
pixels.forEach((pixel) => {
84+
const noScriptElement = this._document.createElement('noscript');
85+
const pixelElement = this._document.createElement('img');
86+
const _pixel = pixel(pixelElement);
87+
noScriptElement.append(_pixel);
88+
this._document.head.appendChild(noScriptElement);
89+
});
90+
}
91+
8392
private initPartyTownScript(): void {
8493
if (!this._config) return;
8594
const config = this._config;
Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
import { DOCUMENT } from '@angular/common';
22
import { inject, Injectable } from '@angular/core';
33

4-
import { ScriptFactory, ScriptsLoader } from './scripts-loader';
4+
import { PixelFactory, ScriptFactory, ScriptsLoader } from './scripts-loader';
55

66
@Injectable()
77
export class ScriptsLoaderService implements ScriptsLoader {
88
private readonly _document = inject(DOCUMENT);
99

10-
init(scripts: ScriptFactory[]): void {
10+
init(scripts: ScriptFactory[], pixels: PixelFactory[]): void {
1111
scripts.forEach((script) => {
1212
const scriptElement = this._document.createElement('script');
1313
const _script = script(scriptElement);
1414
this._document.head.appendChild(_script);
1515
});
16+
17+
pixels.forEach((pixel) => {
18+
const noScriptElement = this._document.createElement('noscript');
19+
const pixelElement = this._document.createElement('img');
20+
const _pixel = pixel(pixelElement);
21+
noScriptElement.append(_pixel);
22+
this._document.head.appendChild(noScriptElement);
23+
});
1624
}
1725
}

libs/blog/tracking/feature/src/lib/scripts-loader.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ export type ScriptFactory = (
22
scriptElement: HTMLScriptElement,
33
) => HTMLScriptElement;
44

5+
export type PixelFactory = (pixel: HTMLElement) => HTMLElement;
6+
57
export abstract class ScriptsLoader {
6-
abstract init(scripts: ScriptFactory[]): void;
8+
abstract init(scripts: ScriptFactory[], pixels: PixelFactory[]): void;
79
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './gtm-script';
22
export * from './meta-script';
3+
export * from './meta-pixel';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { PixelFactory } from '../scripts-loader';
2+
3+
export const metaPixel =
4+
(id: string): PixelFactory =>
5+
(pixel) => {
6+
pixel.setAttribute('height', '1');
7+
pixel.setAttribute('width', '1');
8+
pixel.setAttribute('style', 'display:none');
9+
pixel.setAttribute(
10+
'src',
11+
`https://www.facebook.com/tr?id=${id}&ev=PageView&noscript=1`,
12+
);
13+
14+
return pixel;
15+
};

libs/blog/tracking/feature/src/lib/scripts/meta-script.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ export const metaScript =
44
(id: string): ScriptFactory =>
55
(gtmScript) => {
66
gtmScript.setAttribute('type', 'text/plain');
7-
gtmScript.setAttribute('data-type', 'text/partytown');
87
gtmScript.setAttribute('data-category', 'marketing');
98
gtmScript.textContent = `!function(f,b,e,v,n,t,s)
109
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ import {
1414
PartyTownService,
1515
providePartyTown,
1616
} from './partytown';
17-
import { ScriptFactory, ScriptsLoader } from './scripts-loader';
17+
import { PixelFactory, ScriptFactory, ScriptsLoader } from './scripts-loader';
1818
import { ScriptsLoaderService } from './scripts-loader.service';
1919

2020
export type TrackingConfig = {
2121
partyTown: PartyTownConfig;
2222
cookieConsent: CookieConsentConfig;
2323
scripts?: ScriptFactory[];
24+
pixels?: PixelFactory[];
2425
};
2526

2627
export const provideTracking = (
@@ -52,7 +53,7 @@ export const provideTracking = (
5253

5354
return () => {
5455
if (isPlatformBrowser(platformId)) {
55-
scriptsLoader.init(config.scripts ?? []);
56+
scriptsLoader.init(config.scripts ?? [], config.pixels ?? []);
5657
}
5758
};
5859
},

0 commit comments

Comments
 (0)