Last active
July 20, 2016 01:02
-
-
Save erpe/8586565 to your computer and use it in GitHub Desktop.
another try to get piwik running with turbolinks
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
class @PiwikAnalytics | |
@load: -> | |
# Piwik Analytics depends on a global _paq array. window is the global scope. | |
window._paq = [] | |
window._paq.push(['setTrackerUrl', PiwikAnalytics.trackerUrl() + '/piwik.php']) | |
window._paq.push(['setSiteId', PiwikAnalytics.siteId()]) | |
window._paq.push(['enableLinkTracking']) | |
# Create a script element and insert it in the DOM | |
pa = document.createElement("script") | |
pa.type = "text/javascript" | |
pa.defer = true | |
pa.async = true | |
pa.src = PiwikAnalytics.trackerUrl() + '/piwik.js' | |
firstScript = document.getElementsByTagName("script")[0] | |
firstScript.parentNode.insertBefore pa, firstScript | |
# If Turbolinks is supported, set up a callback to track pageviews on page:change. | |
# If it isn't supported, just track the pageview now. | |
if typeof Turbolinks isnt 'undefined' and Turbolinks.supported | |
document.addEventListener "page:change", (-> | |
PiwikAnalytics.trackPageView() | |
), true | |
else | |
PiwikAnalytics.trackPageView() | |
@trackPageView: (url) -> | |
unless PiwikAnalytics.isLocalRequest() | |
if url | |
window._paq.push ["trackPageView", url] | |
else | |
window._paq.push ["trackPageView"] | |
@trackEcommerceView: (sku) -> | |
unless PiwikAnalytics.isLocalRequest() | |
window._paq.push(['setEcommerceView', sku]) | |
@trackGoal: (id, price=0 ) -> | |
unless PiwikAnalytics.isLocalRequest() | |
window._paq.push(['trackGoal', id, price]) | |
@addEcommerceItem: (sku, name, category, price) -> | |
unless PiwikAnalytics.isLocalRequest() | |
window._paq.push(['addEcommerceItem', sku, name, category, parseFloat(price)]) | |
@trackEcommerceOrder: (order_id, revenue) -> | |
unless PiwikAnalytics.isLocalRequest() | |
window._paq.push(['trackEcommerceOrder', order_id, parseFloat(revenue)]) | |
@trackEcommerceCartUpdate: (sum) -> | |
unless PiwikAnalytics.isLocalRequest() | |
window._paq.push(['trackEcommerceCartUpdate', parseFloat(sum)]) | |
@isLocalRequest: -> | |
if PiwikAnalytics.documentDomainIncludes "localhost" | |
return true | |
if PiwikAnalytics.documentDomainIncludes('127.0.0.1') | |
return true | |
if PiwikAnalytics.documentDomainIncludes('staging') | |
return true | |
@documentDomainIncludes: (str) -> | |
document.domain.indexOf(str) isnt -1 | |
@siteId: -> | |
"1" | |
@trackerUrl: -> | |
proto = '' | |
if document.location.protocol is 'https://' | |
proto = 'https://' | |
else | |
proto = 'http://' | |
proto + "piwik.domain.com" | |
PiwikAnalytics.load() |
Shouldn't line 72 check against 'https:' only?
//EDIT: I polished it up a bit and fixed the URL tracking... Piwik in Ruby on Rails with Turbolinks (and proxy)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
so you can manually use e.g.
PiwikAnalytics.trackPageView(your_page)