Skip to content

Commit 79772fe

Browse files
authored
fix: meta pixel (#254)
1 parent 9511647 commit 79772fe

File tree

5 files changed

+34
-0
lines changed

5 files changed

+34
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { EnvironmentProviders, makeEnvironmentProviders } from '@angular/core';
22

33
import {
44
gtmScript,
5+
metaPixel,
56
metaScript,
67
provideTracking,
78
} from '@angular-love/blog/tracking/feature';
@@ -26,6 +27,7 @@ export const provideAppTracking = (): EnvironmentProviders => {
2627
],
2728
},
2829
scripts: [gtmScript('GTM-5XNT5NS'), metaScript('284876369340184')],
30+
pixels: [metaPixel('284876369340184')],
2931
},
3032
cookieConsent: cookieConsentConfig,
3133
}),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './lib/tracking';
22
export * from './lib/partytown/gtm-script';
33
export * from './lib/partytown/meta-script';
4+
export * from './lib/partytown/meta-pixel';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './gtm-script';
22
export * from './meta-script';
33
export * from './partytown.service';
4+
export * from './meta-pixel';
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { PartyTownPixelFactory } from './partytown.service';
2+
3+
export const metaPixel =
4+
(id: string): PartyTownPixelFactory =>
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/partytown/partytown.service.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type PartyTownConfig = {
2222
proxiedHosts?: string[];
2323
};
2424
scripts?: PartyTownScriptFactory[];
25+
pixels?: PartyTownPixelFactory[];
2526
};
2627

2728
export const PARTY_TOWN_CONFIG = new InjectionToken<PartyTownConfig>(
@@ -46,6 +47,7 @@ export const providePartyTown = (
4647
proxiedHosts: config.partyTown?.proxiedHosts ?? [],
4748
},
4849
scripts: config?.scripts ?? [],
50+
pixels: config?.pixels ?? [],
4951
} satisfies PartyTownConfig,
5052
},
5153
PartyTownService,
@@ -69,6 +71,8 @@ export type PartyTownScriptFactory = (
6971
scriptElement: HTMLScriptElement,
7072
) => HTMLScriptElement;
7173

74+
export type PartyTownPixelFactory = (img: HTMLImageElement) => HTMLImageElement;
75+
7276
@Injectable()
7377
export class PartyTownService {
7478
private readonly _config = inject(PARTY_TOWN_CONFIG, { optional: true });
@@ -78,6 +82,7 @@ export class PartyTownService {
7882
if (this._config?.partyTown?.enabled) {
7983
this.initPartyTownScript();
8084
this.initScripts(...(this._config.scripts ?? []));
85+
this.initPixels(...(this._config.pixels ?? []));
8186
}
8287
}
8388

@@ -89,6 +94,16 @@ export class PartyTownService {
8994
});
9095
}
9196

97+
private initPixels(...scripts: PartyTownPixelFactory[]): void {
98+
scripts.forEach((pixel) => {
99+
const noScriptElement = this._document.createElement('noscript');
100+
const pixelElement = this._document.createElement('img');
101+
const _pixel = pixel(pixelElement);
102+
noScriptElement.append(_pixel);
103+
this._document.head.appendChild(noScriptElement);
104+
});
105+
}
106+
92107
private initPartyTownScript(): void {
93108
if (!this._config?.partyTown) return;
94109
const config = this._config.partyTown;

0 commit comments

Comments
 (0)