]> _ Git - cubist_gtag.git/commitdiff
wip #2951 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 3 Sep 2019 11:20:00 +0000 (13:20 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Tue, 3 Sep 2019 11:20:00 +0000 (13:20 +0200)
resources/public/js/app.js
resources/public/js/gtag.js [new file with mode: 0644]
resources/views/head.blade.php

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/public/js/gtag.js b/resources/public/js/gtag.js
new file mode 100644 (file)
index 0000000..43420e0
--- /dev/null
@@ -0,0 +1,51 @@
+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.event = function (action, category, label, value, noninteraction) {
+    if (noninteraction === undefined) {
+        noninteraction = false;
+    }
+    let 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;
+    }
+    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();
index f2e55483b09a9f28ee6650a21a9d17b9a7e902a4..67b6b2295ce26f0cce59d8157e5ec4bfd4d309b6 100644 (file)
@@ -9,6 +9,6 @@
         }
 
         gtag('js', new Date());
-        gtag('config', '{{$id}}');
+        gtag('config', '{{$id}}', {'anonymize_ip': true});
     </script>
 @endif