Skip to content

Commit

Permalink
Try initializing GTM in a way that works runtime (#1921)
Browse files Browse the repository at this point in the history
## 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
  • Loading branch information
brianjgeiger authored Aug 8, 2023
1 parent 85e7171 commit c767345
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config/environment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ declare const config: {
rootURL: string;
assetsPrefix: string;
sentryDSN: string | null;
googleTagManagerId: string | null;
sentryOptions: {
release?: string;
ignoreErrors: string[];
Expand Down Expand Up @@ -195,7 +196,6 @@ declare const config: {
pageTitle: {
prepend: boolean;
};
gtm: string;
};

export default config;
2 changes: 1 addition & 1 deletion config/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ module.exports = function(environment) {
assetsPrefix,
locationType: 'auto',
sentryDSN: null,
googleTagManagerId: null,
sentryOptions: {
release,
ignoreErrors: [
Expand Down Expand Up @@ -303,7 +304,6 @@ module.exports = function(environment) {
pageTitle: {
prepend: false,
},
gtm: '<script></script>',
};

if (environment === 'development') {
Expand Down
13 changes: 12 additions & 1 deletion ember-cli-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,18 @@ module.exports = function(defaults) {
},
gtm: {
enabled: IS_PROD,
content: config.gtm,
content: `<script>
var configJson = document.head.querySelector("meta[name$='/config/environment']").content;
var configObject = JSON.parse(unescape(configJson));
if (configObject.googleTagManagerId) {
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', configObject.googleTagManagerId);
}
</script>
`,
postProcess,
},
},
Expand Down

0 comments on commit c767345

Please sign in to comment.