start-google-analytics-tracker
is a Polymer Element for Google Analytics Web Tracking, supports page and event tracking.
Script analytics.js is served from local scripts
dir, because https://www.google-analytics.com/analytics.js have only 2 hours cache.
You need download latest analytics.js file.
wget https://www.google-analytics.com/analytics.js -O scripts/analytics.js
Inspired by this repo https://github.com/matthewlawson/google-analytics-universal-tracker
bower install start-google-analytics-tracker --save
<link rel="import" href="bower_components/start-google-analytics-tracker/start-google-analytics-tracker.html">
<start-google-analytics-tracker code="UA-XXXXX-Y"></start-google-analytics-tracker>
If you are using regular anchor links and not push-state links you are ready to go!
If you have a Single Page Application that uses push-state, eg with page.js
or <app-router>
you will need to signal to the start-google-analytics-tracker
element that the page has changed.
The Page Change events are handled using iron-signals so you do not need to do any dom finding to trigger a page track.
this.fire('iron-signal', {name: 'track-page', data: { path: "/about.html" } });
this.fire('iron-signal', {name: 'track-event',data: {category: "messages",action: "send_text_message",label: "group",value: 1}});
To use Google Analytics user id attribution set the user id property on the element:
document.querySelector("start-google-analytics-tracker").userId = loggedInUserId;