Skip to content

Commit c767345

Browse files
authored
Try initializing GTM in a way that works runtime (#1921)
## Purpose You can't just pull something into the index.html from runtime, it has to happen build time. So we'll try doing this the way raven works instead. ## Summary of Changes 1. Initialize GTM in a way that works runtime. 2. Add environment variable for the gtm id 3. Types
1 parent 85e7171 commit c767345

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

config/environment.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ declare const config: {
2828
rootURL: string;
2929
assetsPrefix: string;
3030
sentryDSN: string | null;
31+
googleTagManagerId: string | null;
3132
sentryOptions: {
3233
release?: string;
3334
ignoreErrors: string[];
@@ -195,7 +196,6 @@ declare const config: {
195196
pageTitle: {
196197
prepend: boolean;
197198
};
198-
gtm: string;
199199
};
200200

201201
export default config;

config/environment.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ module.exports = function(environment) {
7777
assetsPrefix,
7878
locationType: 'auto',
7979
sentryDSN: null,
80+
googleTagManagerId: null,
8081
sentryOptions: {
8182
release,
8283
ignoreErrors: [
@@ -303,7 +304,6 @@ module.exports = function(environment) {
303304
pageTitle: {
304305
prepend: false,
305306
},
306-
gtm: '<script></script>',
307307
};
308308

309309
if (environment === 'development') {

ember-cli-build.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,18 @@ module.exports = function(defaults) {
108108
},
109109
gtm: {
110110
enabled: IS_PROD,
111-
content: config.gtm,
111+
content: `<script>
112+
var configJson = document.head.querySelector("meta[name$='/config/environment']").content;
113+
var configObject = JSON.parse(unescape(configJson));
114+
if (configObject.googleTagManagerId) {
115+
window.dataLayer = window.dataLayer || [];
116+
function gtag(){dataLayer.push(arguments);}
117+
gtag('js', new Date());
118+
119+
gtag('config', configObject.googleTagManagerId);
120+
}
121+
</script>
122+
`,
112123
postProcess,
113124
},
114125
},

0 commit comments

Comments
 (0)