Skip to content

Commit

Permalink
Merge pull request #40 from FrittenKeeZ/master
Browse files Browse the repository at this point in the history
Prevent dataLayer entries from being reset
  • Loading branch information
freekmurze committed Nov 17, 2021
2 parents 0264f97 + c662bf8 commit d28e9b9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
6 changes: 4 additions & 2 deletions resources/views/head.blade.php
@@ -1,9 +1,11 @@
@if($enabled)
<script>
window.dataLayer = window.dataLayer || [];
dataLayer = [{!! $dataLayer->toJson() !!}];
@unless(empty($dataLayer->toArray()))
window.dataLayer.push({!! $dataLayer->toJson() !!});
@endunless
@foreach($pushData as $item)
dataLayer.push({!! $item->toJson() !!});
window.dataLayer.push({!! $item->toJson() !!});
@endforeach
</script>
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
Expand Down
7 changes: 4 additions & 3 deletions src/GoogleTagManager.php
Expand Up @@ -2,6 +2,7 @@

namespace Spatie\GoogleTagManager;

use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Macroable;

class GoogleTagManager
Expand Down Expand Up @@ -41,7 +42,7 @@ public function __construct($id)
$this->id = $id;
$this->dataLayer = new DataLayer();
$this->flashDataLayer = new DataLayer();
$this->pushDataLayer = new \Illuminate\Support\Collection();
$this->pushDataLayer = new Collection();

$this->enabled = true;
}
Expand All @@ -59,7 +60,7 @@ public function id()
public function setId($id)
{
$this->id = $id;

return $this;
}

Expand Down Expand Up @@ -160,7 +161,7 @@ public function getPushData()
public function clear()
{
$this->dataLayer = new DataLayer();
$this->pushDataLayer = new \Illuminate\Support\Collection();
$this->pushDataLayer = new Collection();
}

/**
Expand Down

0 comments on commit d28e9b9

Please sign in to comment.