Data Layer Implement in laravel
<!-- Google Tag Manager -->
//need to add it front.blade.php
<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],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-yyuyuytg');</script>
<!-- End Google Tag Manager -->
//need to add this code in product details blade file page.
{{-- data layer implementation code for view items --}}
<script type = "text/javascript">
dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object.
dataLayer.push({
event : "view_item",
ecommerce: {
items: [{
item_name : "{{$productt->name}}", // Name or ID is required.
item_id : {{$productt->id}},
price : "{{$productt->showPrice()}}",
item_category : "{{$productt->category->name ?? ""}}",
item_variant : "",
item_list_name: "", // If associated with a list selection.
item_list_id : "", // If associated with a list selection.
index : 0, // If associated with a list selection.
quantity : {{ $productt['qty'] ?? 0 }}
}]
}
});
</script>
{{-- Add to Cart data layer implement code --}}
<li class="addtocart">
<a href="javascript:;" data-id="{{ $productt->id }}" id="addcrt"
onclick="addToCartData({{ json_encode($productt->toArray() + ['formatted_price' => $productt->showPrice()]) }})"><i class="icofont-cart"></i>{{ $langg->lang90 }}</a>
</li>
<script type = "text/javascript">
function addToCartData (data) {
console.log('data', data);
dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object.
dataLayer.push({
event : "add_to_cart",
ecommerce: {
items: [{
item_name : data.name, // Name or ID is required.
item_id : data.id,
price : data.formatted_price,
item_category : data['category'] ? data['category']['name'] : "",
item_variant : "",
item_list_name: "", // If associated with a list selection.
item_list_id : "", // If associated with a list selection.
index : 0, // If associated with a list selection.
quantity : data.qty ? data.qty : 1,
}]
}
});
}
</script>
//need to add this code in checkout.blade.php file
{{-- data layer implementation code for purchase product --}}
<script type = "text/javascript">
dataLayer.push({ ecommerce: null }); // Clear the previous ecommerce object.
dataLayer.push({
event : "begin_checkout",
ecommerce: {
items: [@foreach ($products as $product){
item_name : "{{ $product['item']['name'] }}",
item_id : "{{$product['item']['id']}}",
price : "{{ $product['item']->showPrice() }}",
item_category : "{{ optional($product['item']->category)->name }}",
item_variant : "",
item_list_name: "", // If associated with a list selection.
item_list_id : "", // If associated with a list selection.
index : 0, // If associated with a list selection.
quantity : {{ $product['qty'] ?? 0 }}
},@endforeach]
}
});
</script>
No comments