From 632f77df1248a4c1ceaf4636fce9e38355e5daf2 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Mon, 2 Sep 2019 18:41:47 +0200 Subject: [PATCH] wip #2951 --- composer.json | 4 ++-- resources/public/js/app.js | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 resources/public/js/app.js diff --git a/composer.json b/composer.json index cc7e2dd..7226ef4 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "cubist\/gtag", + "name": "cubist/gtag", "description": "Google analytics tracking code for Laravel", "type": "library", "license": "proprietary", @@ -22,7 +22,7 @@ ], "require": { "php": ">=5.5.0", - "laravel/framework": "5.8.*" + "laravel/framework": "^5.8" }, "repositories": [ { diff --git a/resources/public/js/app.js b/resources/public/js/app.js new file mode 100644 index 0000000..58dff40 --- /dev/null +++ b/resources/public/js/app.js @@ -0,0 +1,38 @@ +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) + } +} -- 2.39.5