-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8e14c4
commit c70192c
Showing
3 changed files
with
115 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# EditorConfig helps developers define and maintain consistent | ||
# coding styles between different editors and IDEs | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
|
||
[*] | ||
|
||
# Change these settings to your own preference | ||
indent_style = space | ||
indent_size = 4 | ||
|
||
# We recommend you to keep these unchanged | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,68 +1,97 @@ | ||
'use strict'; | ||
|
||
(function(root) { | ||
function DataMetrics() {} | ||
|
||
DataMetrics.prototype = { | ||
init: function() { | ||
this.addEventListeners(); | ||
}, | ||
|
||
addEventListeners: function() { | ||
var self = this; | ||
var elements = document.querySelectorAll('*[data-metrics]'); | ||
|
||
for (var i = 0; i < elements.length; i++) { | ||
if ( elements[i].tagName !== 'FORM' ) { | ||
elements[i].onclick = function(e) { | ||
self.appendEvent.call(self, e); | ||
}; | ||
} else { | ||
elements[i].onsubmit = function(e) { | ||
self.appendEvent.call(self, e); | ||
}; | ||
} | ||
} | ||
}, | ||
|
||
appendEvent: function (e) { | ||
var elem = e.currentTarget; | ||
|
||
if (!elem.classList.contains('sending-metrics')) { | ||
var params = elem.getAttribute('data-metrics').split('|'); | ||
|
||
if (params[1] === undefined) params[1] = null; | ||
if (params[2] === undefined) params[2] = null; | ||
|
||
this.sendToGA(params[0], params[1], params[2], elem); | ||
elem.classList.add('sending-metrics'); | ||
return false; | ||
} | ||
}, | ||
|
||
sendToGA: function(category, action, label, elem) { | ||
var self = this; | ||
|
||
window.ga('send', 'event', category, action, label, { 'hitCallback': function() { | ||
if (elem) { | ||
self.onEventIsDispatched.call(self, elem); | ||
} | ||
}}); | ||
}, | ||
(function (ga) { | ||
function DataMetrics() { | ||
this.init(); | ||
} | ||
|
||
onEventIsDispatched: function (elem) { | ||
if (elem.getAttribute('href')) { | ||
window.location.href = elem.getAttribute('href'); | ||
} else if (elem.tagName === 'FORM') { | ||
elem.submit(); | ||
} | ||
DataMetrics.prototype = { | ||
init: function () { | ||
this.addEventListeners(); | ||
}, | ||
|
||
elem.classList.remove('sending-metrics'); | ||
} | ||
}; | ||
addEventListeners: function () { | ||
var elements = document.querySelectorAll('*[data-metrics]'); | ||
for (var i = 0; i < elements.length; i++) { | ||
if (!this.isSubmitInput(elements[i])) { | ||
elements[i].addEventListener('click', this.appendEvent.bind(this), false); | ||
} else { | ||
elements[i].onsubmit = this.appendEvent.bind(this); | ||
} | ||
} | ||
}, | ||
|
||
isSubmitInput: function (elem) { | ||
if (elem.tagName === 'FORM') { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}, | ||
|
||
isLink: function (elem) { | ||
if (elem.getAttribute('href')) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}, | ||
|
||
linkHasBlank: function (elem) { | ||
if (elem.getAttribute('target')) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
}, | ||
|
||
appendEvent: function (e) { | ||
var elem = e.currentTarget; | ||
|
||
if (!elem.classList.contains('sending-metrics')) { | ||
var params = elem.getAttribute('data-metrics').split('|'); | ||
|
||
if (params[1] === undefined) { | ||
params[1] = null; | ||
} | ||
if (params[2] === undefined) { | ||
params[2] = null; | ||
} | ||
this.sendToGA(params[0], params[1], params[2], elem); | ||
elem.classList.add('sending-metrics'); | ||
|
||
if (!this.isSubmitInput(elem) && !this.linkHasBlank(elem)) { | ||
e.preventDefault(); | ||
return false; | ||
} | ||
} | ||
}, | ||
|
||
sendToGA: function (category, action, label, elem) { | ||
var self = this; | ||
window.ga('send', 'event', category, action, label, { 'hitCallback': function () { | ||
self.onHitCallback(elem); | ||
}}); | ||
}, | ||
|
||
onHitCallback: function (elem) { | ||
if (elem) { | ||
this.onEventIsDispatched(elem); | ||
} | ||
}, | ||
|
||
onEventIsDispatched: function (elem) { | ||
if (this.isLink(elem) && !this.linkHasBlank(elem)) { | ||
window.location.href = elem.getAttribute('href'); | ||
} else if (this.isSubmitInput(elem)) { | ||
elem.submit(); | ||
} | ||
|
||
elem.classList.remove('sending-metrics'); | ||
} | ||
}; | ||
|
||
root.dataMetrics = new DataMetrics(); | ||
root.dataMetrics.init(); | ||
window.dataMetrics = new DataMetrics(); | ||
|
||
})(this); | ||
})(window.ga); | ||
|