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');
--- /dev/null
+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();