Skip to content

Commit 838938d

Browse files
authored
refactor(blog): change gtm initialization mechanism (#433)
1 parent 7691064 commit 838938d

File tree

5 files changed

+15
-19
lines changed

5 files changed

+15
-19
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ export const cookieConsentConfig: CreateCookieConsentConfigFn = (cc) => {
8282
},
8383
onConsent: () => {
8484
updateGtagConsent(cc);
85+
cc.loadScript('/assets/scripts/gtm.js');
8586
},
8687
onChange: () => {
8788
updateGtagConsent(cc);

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

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

33
import {
4-
gtmScript,
54
metaPixel,
65
metaScript,
76
provideTracking,
@@ -31,7 +30,7 @@ export const provideAppTracking = (): EnvironmentProviders => {
3130
],
3231
},
3332
cookieConsent: cookieConsentConfig,
34-
scripts: [gtmScript('GTM-5XNT5NS'), metaScript('284876369340184')],
33+
scripts: [metaScript('284876369340184')],
3534
pixels: [metaPixel('284876369340184')],
3635
}),
3736
]);

apps/blog/src/assets/scripts/gtm.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(function (w, d, s, l, i) {
2+
w[l] = w[l] || [];
3+
w[l].push({ 'gtm.start': new Date().getTime(), event: 'gtm.js' });
4+
var f = d.getElementsByTagName(s)[0],
5+
j = d.createElement(s),
6+
dl = l != 'dataLayer' ? '&l=' + l : '';
7+
j.defer = true;
8+
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
9+
f.parentNode.insertBefore(j, f);
10+
})(window, document, 'script', 'dataLayer', 'GTM-5XNT5NS');

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

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,6 @@
11
import type * as CookieConsent from 'vanilla-cookieconsent';
22

33
import { CATEGORIES, SERVICES } from '../const';
4-
import { ScriptFactory } from '../scripts-loader';
5-
6-
export const gtmScript =
7-
(id: string): ScriptFactory =>
8-
(gtmScript) => {
9-
gtmScript.setAttribute('type', 'text/plain');
10-
gtmScript.setAttribute('data-category', 'necessary');
11-
gtmScript.textContent = `(function (w, d, s, l, i) {
12-
w[l] = w[l] || [];
13-
w[l].push({'gtm.start': new Date().getTime(), event: 'gtm.js'});
14-
var f = d.getElementsByTagName(s)[0], j = d.createElement(s), dl = l != 'dataLayer' ? '&l=' + l : '';
15-
j.defer = true;
16-
j.src = 'https://www.googletagmanager.com/gtm.js?id=' + i + dl;
17-
f.parentNode.insertBefore(j, f);
18-
})(window, document, 'script', 'dataLayer', '${id}');`;
19-
return gtmScript;
20-
};
214

225
type PushFn = (...args: any[]) => void;
236
type DataLayer = {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import { CATEGORIES } from '../const';
12
import { ScriptFactory } from '../scripts-loader';
23

34
export const metaScript =
45
(id: string): ScriptFactory =>
56
(gtmScript) => {
7+
gtmScript.setAttribute('type', 'text/plain');
8+
gtmScript.setAttribute('data-category', CATEGORIES.ANALYTICS);
69
gtmScript.textContent = `!function(f,b,e,v,n,t,s)
710
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
811
n.callMethod.apply(n,arguments):n.queue.push(arguments)};

0 commit comments

Comments
 (0)