version: 0.6.4
data-metrics is a data-attribute that allows you to easily track Google Analytics metrics in your HTML page.
See examples of use here.
bower install data-metrics
Add your Google Analytics tag:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-X', 'your URL');
ga('send', 'pageview');
</script>
Replace 'UA-XXXXXXXX-X'
with your GA id.
'your URL'
Insert your URL domain or set it as 'none'
for local testing.
Insert the data-metrics script.
<script type="text/javascript" src="bower_components/data-metrics/dist/data-metrics.js"></script>
Add the data-metrics attribute and its values on the element to be tracked.
<button data-metrics="Documents|Click|JavaScript Ebook">Add metrics</button>
Browse other examples here.
You can also use the programatically API to track custom items.
Vanilla JavaScript
document.querySelector('.click-image').onclick = function() {
dataMetrics.sendToGA('Images', 'Click', 'Marlos Carmo');
}
jQuery
$('.click-image').click(function() {
dataMetrics.sendToGA('Images', 'Click', 'Marlos Carmo');
});
See more examples of data-metrics usage
- Marlos Carmo @marloscarmo
- Erick Belfort @erickbelfy
- Ruy Adorno @ruyadorno
Licensed under the MIT License