From: Vincent Vanwaelscappel Date: Tue, 3 Sep 2019 14:47:36 +0000 (+0200) Subject: wip #2951 @1 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=ac3e7066b2fb547ae96fc573f6e1625934d9d136;p=pmi.git wip #2951 @1 --- diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index 23700ed..66134db 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -140,6 +140,11 @@ class AjaxController extends CubistFrontController // Get existing session or an empty array $cart_items = $request->session()->get('cart_items', []); + $ga['add'] = []; + $ga['remove'] = []; + + $needs_update=false; + switch ($request->input('action')) { case 'add': // If the item already exists in the cart, increment the quantity @@ -149,6 +154,7 @@ class AjaxController extends CubistFrontController $cart_items[$id] = $quantity; $needs_update = true; } + $ga['add'][$id] = $quantity; break; case 'update': @@ -168,7 +174,12 @@ class AjaxController extends CubistFrontController break; case 'delete': - unset($cart_items[$id]); + if(isset($cart_items[$id])) { + $ga['remove'][$id] = $cart_items[$id]; + unset($cart_items[$id]); + } + $needs_update = true; + break; } diff --git a/app/Models/Product.php b/app/Models/Product.php index c6d4f34..89d183b 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -359,6 +359,7 @@ class Product extends CubistMagicPageModel $cart_data[] = [ 'id' => $product->id, 'name' => $product->name, + 'reference'=>$product->reference, 'category' => $product->type->name, 'quantity' => $cart_items[$product->id], 'image' => $product->image, diff --git a/resources/js/vendor/cubist/gtag/app.js b/resources/js/vendor/cubist/gtag/app.js index 58dff40..316f2c8 100644 --- a/resources/js/vendor/cubist/gtag/app.js +++ b/resources/js/vendor/cubist/gtag/app.js @@ -1,38 +1,2 @@ require('element-closest'); - -document.addEventListener('DOMContentLoaded', function () { - Array.prototype.forEach.call(document.querySelectorAll('meta[data-ga]'), function (el, i) { - handleGtag(el) - }); -}); - -document.addEventListener('click', function (e) { - - if (e.target.matches('[data-ga]')) { - handleGtag(e.target); - } - if (e.target.closest('[data-ga]')) { - handleGtag(e.target.closest('[data-ga]')); - } -}, false); - -function handleGtag(el) { - if (el.getAttribute('data-ga') === 'event') { - console.log(el); - let action = el.getAttribute('data-ga-action'); - let category = el.getAttribute('data-ga-category'); - let label = el.getAttribute('data-ga-label'); - let value = el.getAttribute('data-ga-value'); - let options = {non_interaction: el.getAttribute('data-ga-noninteraction') === 1}; - if (null !== category) { - options.event_category = category; - } - if (null !== label) { - options.event_label = label; - } - if (null !== value) { - options.value = value; - } - gtag('event', action, options) - } -} +window.cubistga = require('./gtag'); diff --git a/resources/js/vendor/cubist/gtag/gtag.js b/resources/js/vendor/cubist/gtag/gtag.js new file mode 100644 index 0000000..31119e2 --- /dev/null +++ b/resources/js/vendor/cubist/gtag/gtag.js @@ -0,0 +1,71 @@ +function cubistga() { + this.initEvents(); +} + +cubistga.prototype.initEvents = function () { + var $this = this; + document.addEventListener('DOMContentLoaded', function () { + Array.prototype.forEach.call(document.querySelectorAll('meta[data-ga]'), function (el, i) { + $this.handleGtag(el) + }); + }); + + document.addEventListener('click', function (e) { + if (e.target.matches('[data-ga]')) { + $this.handleGtag(e.target); + } + if (e.target.closest('[data-ga]')) { + $this.handleGtag(e.target.closest('[data-ga]')); + } + }, false); +}; + +cubistga.prototype.addToCart = function (id, name, quantity) { + this.event('add_to_cart', 'ecommerce', null, null, false, { + items: [ + {id: id, name: name, quantity: quantity} + ] + }); +}; + +cubistga.prototype.removeFromCart = function (id, name, quantity) { + this.event('remove_from_cart', 'ecommerce', null, null, false, { + items: [ + {id: id, name: name, quantity: quantity} + ] + }); +}; + +cubistga.prototype.event = function (action, category, label, value, noninteraction, options) { + if (noninteraction === undefined) { + noninteraction = false; + } + if (options === undefined) { + options = {}; + } + options.non_interaction = noninteraction; + if (undefined !== category && null !== category) { + options.event_category = category; + } + if (undefined !== label && null !== label) { + options.event_label = label; + } + if (undefined !== value && null !== value) { + options.value = value; + } + console.log('gtag event',action,options); + return gtag('event', action, options) +}; + +cubistga.prototype.handleGtag = function (el) { + if (el.getAttribute('data-ga') === 'event') { + let action = el.getAttribute('data-ga-action'); + let category = el.getAttribute('data-ga-category'); + let label = el.getAttribute('data-ga-label'); + let value = el.getAttribute('data-ga-value'); + + return this.event(action, category, label, value, el.getAttribute('data-ga-noninteraction') === 1); + } +}; + +module.exports = new cubistga();