]> _ Git - pmi.git/commitdiff
wip #2951 @1
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 3 Sep 2019 14:47:36 +0000 (16:47 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 3 Sep 2019 14:47:36 +0000 (16:47 +0200)
app/Http/Controllers/AjaxController.php
app/Models/Product.php
resources/js/vendor/cubist/gtag/app.js
resources/js/vendor/cubist/gtag/gtag.js [new file with mode: 0644]

index 23700ed2fe5aa7659ed8711a706e6c55341f6aae..66134db81e2986fb3553e1e49b4be63f4efc816d 100644 (file)
@@ -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;
         }
 
index c6d4f34c295920c9f4f8af22e9f162cf912d6829..89d183b49448177171f9a7b9b3d0ab110f35870f 100644 (file)
@@ -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,
index 58dff40b1661534fbf479cddaa6de19dab868c1d..316f2c8b17b660735cacf12a6b5537ffdff36ad4 100644 (file)
@@ -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 (file)
index 0000000..31119e2
--- /dev/null
@@ -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();