Skip to content

Commit

Permalink
Merge pull request #50 from mickaelperrin/pushOnLoad
Browse files Browse the repository at this point in the history
👌 IMPROVE: delay push after load event
  • Loading branch information
freekmurze authored Dec 11, 2023
2 parents faac69d + ac19446 commit 33cc32c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
12 changes: 12 additions & 0 deletions resources/views/body.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
@if($enabled)
<script>
function gtmPush() {
@unless(empty($dataLayer->toArray()))
window.dataLayer.push({!! $dataLayer->toJson() !!});
@endunless
@foreach($pushData as $item)
window.dataLayer.push({!! $item->toJson() !!});
@endforeach
}
addEventListener("load", gtmPush);
</script>
<noscript><iframe src="https://www.googletagmanager.com/ns.html?id={{ $id }}"
height="0" width="0" style="display:none;visibility:hidden"></iframe></noscript>
@endif

6 changes: 0 additions & 6 deletions resources/views/head.blade.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
@if($enabled)
<script>
window.dataLayer = window.dataLayer || [];
@unless(empty($dataLayer->toArray()))
window.dataLayer.push({!! $dataLayer->toJson() !!});
@endunless
@foreach($pushData as $item)
window.dataLayer.push({!! $item->toJson() !!});
@endforeach
</script>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
Expand Down

0 comments on commit 33cc32c

Please sign in to comment.