Skip to content

Commit

Permalink
fix(plugin): load SSR events before container loaded (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
1ed committed Jul 23, 2020
1 parent 4894d7c commit 498f9bd
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions lib/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function gtmClient(ctx, initialized) {
},
push(obj) {
if (!window[_layer]) {
window[_layer] = [{ 'gtm.start': new Date().getTime(), event: 'gtm.js' }]
window[_layer] = []
}
window[_layer].push(obj)
}
Expand All @@ -29,6 +29,8 @@ function gtmServer(ctx, initialized) {
}

const gtmScript = ctx.app.head.script.find(s => s.hid == '<%= options.scriptId %>')
gtmScript.innerHTML = `window['${_layer}']=${JSON.stringify(events)};${gtmScript.innerHTML}`

if (inits.length) {
gtmScript.innerHTML += `;${JSON.stringify(inits)}.forEach(function(i){window._gtm_inject(i)})`
}
Expand All @@ -39,9 +41,6 @@ function gtmServer(ctx, initialized) {
gtmIframe.innerHTML += inits.map(renderIframe)
}
<% } %>
if (events.length) {
gtmScript.innerHTML += `;${JSON.stringify(events)}.forEach(function(e){window['${_layer}'].push(e)})`
}
})

return {
Expand Down

0 comments on commit 498f9bd

Please sign in to comment.