Skip to content

Commit

Permalink
👌 IMPROVE: delay push after load event
Browse files Browse the repository at this point in the history
This is helpful to handle privacy policies framework choices before GTM data is pushed.

Signed-off-by: Mickaël Perrin <[email protected]>
  • Loading branch information
mickaelperrin committed Dec 11, 2023
1 parent faac69d commit ac19446
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 ac19446

Please sign in to comment.