Last active
December 31, 2015 13:49
-
-
Save erpe/7995523 to your computer and use it in GitHub Desktop.
my try to get turbolinks work with (piwik) javascript tracking
somehow its not relayable - dunno why... it tracks sometimes - but not to the expected degree...
figured out, that tracking works as today in firefox 26.0 on Linux as well as on Android Standard (safari-based?) Browser (4.3) - but on Chrome Version 31.0.1650.63 on Linux as well as an…
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
<!-- app/views/shared/_piwik_js.html.erb --> | |
<script type="text/javascript" data-turbolinks-eval=false> | |
var _paq = _paq || []; | |
_paq.push(["trackPageView"]); | |
_paq.push(["enableLinkTracking"]); | |
(function() { | |
var u=(("https:" == document.location.protocol) ? "https" : "http") + "://<%= PiwikSetup.host %>/"; | |
_paq.push(["setTrackerUrl", u+"piwik.php"]); | |
_paq.push(["setSiteId", "<%= PiwikSetup.site_id %>"]); | |
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript"; | |
g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s); | |
})(); | |
</script> | |
<!-- End PiwikCode --> |
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
<!-- app/views/layouts/application.html.erb --> | |
<!doctype html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title><%= content_for?(:title) ? yield(:title) : "example" %></title> | |
<meta name="description" content=""> | |
<meta name="author" content=""> | |
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> | |
<%= javascript_include_tag "application", "data-turbolinks-track" => true %> | |
<%= csrf_meta_tags %> | |
<%= yield(:head) %> | |
</head> | |
<body> | |
<div class='container'> | |
<%= render( :partial => 'shared/messages' ) %> | |
<%= yield %> | |
</div> | |
<%= piwik_tracking_code %> | |
</body> | |
</html> |
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
# app/helpers/application_helper.rb | |
module ApplicationHelper | |
def piwik_tracking_code | |
if PiwikSetup.enabled? | |
render(partial: 'shared/piwik_js') | |
else | |
"<!-- Piwik Tracking disabled -->".html_safe | |
end | |
end | |
end |
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
# app/assets/javascripts/piwik.coffee | |
doTrackPiwik = -> | |
if window._paq? | |
_paq.push ['trackPageView'] | |
else if window.piwikTracker? | |
piwikTracker.trackPageView() | |
unless window._paq? || window.piwikTracker? | |
console.log "piwik-tracking does not work" | |
$(document).on('page:change', doTrackPiwik) |
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
# config/piwik.yml | |
# Configuration: | |
# | |
# url | |
# The url of your piwik instance (e.g. localhost/piwik/ | |
# id_site | |
# The id of your website inside Piwik | |
# enabled | |
# boolean if tracking should be enabled | |
# | |
production: | |
site_id: 3 | |
url: stats.example.com | |
enabled: true | |
staging: | |
site_id: 4 | |
url: stats.example.com | |
enabled: true | |
development: | |
site_id: 4 | |
url: stats.example.com | |
enabled: true | |
test: | |
site_id: 1 | |
url: stats.example.com | |
enabled: 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
# config/initializer/piwik_setup.rb | |
module PiwikSetup | |
def self.config | |
yml = YAML.load_file(File.join(Rails.root,'config','piwik.yml')) | |
@_pwk_cfg = yml[Rails.env] | |
end | |
def self.enabled? | |
@_pwk_cfg ? @_pwk_cfg['enabled'] == true : self.config['enabled'] == true | |
end | |
def self.site_id | |
@_pwk_cfg ? @_pwk_cfg['site_id'] : self.config['site_id'] | |
end | |
def self.host | |
@_pwk_cfg ? @_pwk_cfg['url'] : self.config['url'] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The problem is that Piwik gets the current URL only once when init the Piwik JS object. Make a call to setCustomUrl passing the current URL just before the trackPageView
Also it may be worth creating a ticket at dev.piwik.org as I think it is a bug (ie. maybe we should read URL every time trackPageView is called )