Basic Google Analytics web component with Do Not Track support.
<ga-dnt-analytics debug="true"></ga-dnt-analytics>
ga-dnt-analytics - Open DevTools and check console.
Install the component using Bower:
bower i justinribeiro/ga-dnt-analytics --save
-
Import Web Components' polyfill, if needed:
<script src="bower_components/webcomponentsjs/webcomponents-loader.js"></script>
-
Import ga-dnt-analytics:
<link rel="import" href="bower_components/ga-dnt-analytics/ga-dnt-analytics.html"/>
-
Start using it!
<ga-dnt-analytics key="UA-XXXXXX-X"></ga-dnt-analytics>
Attribute | Options | Default | Description |
---|---|---|---|
key |
String | "" | (optional) Sets UA for Google Analytics tracking |
debug |
Boolean | false |
(optional) Enables Google Analytics debugging mode |
trace |
Boolean | false |
(optional) Use with debug; enables full tracing for GA |
donottrack |
Boolean | true |
(optional) Check and use Do Not Track browser flag |
pageview |
Boolean | false |
(optional) Send ga('send', 'pageview') ping on element stamp |
If not using the pageview
property to send a ping to GA, you can use the send()
method to send a payload to GA.
// via Polymer 2.x
this.shadowRoot.querySelector('ga-dnt-analytics').send({
hitType: 'pageview',
page: window.location.pathname,
location: window.location.href,
title: 'My Title'
});
// via Polymer 1.x
this.$$('ga-dnt-analytics').send({
hitType: 'pageview',
page: window.location.pathname,
location: window.location.href,
title: 'My Title'
});
// via JavaScript
document.querySelector('ga-dnt-analytics').send({
hitType: 'pageview',
page: window.location.pathname,
location: window.location.href,
title: 'My Title'
});
For list of payloads and string/objects to send, see See https://developers.google.com/analytics/devguides/collection/analyticsjs/sending-hits.
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
Justin Ribeiro Schalk Neethling Google Analytics
MIT License (MIT)