Skip to content

Commit

Permalink
fix: initialization check when only using runtimeId
Browse files Browse the repository at this point in the history
  • Loading branch information
alexan committed Apr 6, 2022
1 parent ae9199e commit 14308ed
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function gtmClient(ctx, initialized) {
}
}


function gtmServer(ctx, initialized) {
const events = []
const inits = []
Expand All @@ -38,13 +39,17 @@ function gtmServer(ctx, initialized) {
if (inits.length) {
gtmScript.innerHTML += `;${JSON.stringify(inits)}.forEach(function(i){window._gtm_inject(i)})`
}
<% if (options.noscript) { %>
const gtmIframe = ctx.app.head.noscript.find(s => s.hid == '<%= options.noscriptId %>')
const renderIframe = id => `<%= options.renderIframe('${id}') %>`
if (inits.length) {
gtmIframe.innerHTML += inits.map(renderIframe)
}
<% } %>
<% if (options.noscript) { %>
const gtmIframe = ctx.app.head.noscript.find(s => s.hid == '<%= options.noscriptId %>')
const renderIframe = id => `<%= options.renderIframe('${id}') %>`
if (inits.length) {
gtmIframe.innerHTML += inits.map(renderIframe)
}
<% } %>

const initialized = Object.fromEntries(inits.map(i => [i, true]))

gtmScript.innerHTML += `;window._gtm_initialized = ${JSON.stringify(initialized)}`
})

return {
Expand Down Expand Up @@ -82,7 +87,11 @@ export default function (ctx, inject) {
const autoInit = <%= options.autoInit %>
const id = '<%= options.id %>'
const runtimeId = runtimeConfig.id
const initializedId = id || runtimeId
const initialized = autoInit && id ? {[id]: true} : {}
if (process.client) {
Object.assign(initialized, window._gtm_initialized)
}
const $gtm = process.client ? gtmClient(ctx, initialized) : gtmServer(ctx, initialized)
if (autoInit && runtimeId && runtimeId !== id) {
$gtm.init(runtimeId)
Expand Down

0 comments on commit 14308ed

Please sign in to comment.