Move piwik code to a static JS asset
authorTom Hughes <tom@compton.nu>
Wed, 5 Sep 2012 22:05:07 +0000 (23:05 +0100)
committerTom Hughes <tom@compton.nu>
Thu, 6 Sep 2012 08:02:51 +0000 (09:02 +0100)
app/assets/javascripts/application.js
app/assets/javascripts/globals.js.erb
app/assets/javascripts/piwik.js [new file with mode: 0644]
app/views/layouts/_head.html.erb
app/views/layouts/_piwik.html.erb [deleted file]
app/views/layouts/site.html.erb

index 747c6d9afe5d45544717c7f24c759ee5f8bfd77a..f337415c699344e09b74abcf6253223076f80bb7 100644 (file)
@@ -6,6 +6,7 @@
 //= require openlayers
 //= require i18n/translations
 //= require globals
+//= require piwik
 //= require browse
 //= require export
 //= require map
index 0f1b2b8ce8a236fa46a38edd7ec7bec811ba301b..3c3bd92a7e83bafbffb6f7aaf2e50b2aeb69e79c 100644 (file)
@@ -1,4 +1,8 @@
 OSM = {
+<% if defined?(PIWIK_LOCATION) and defined?(PIWIK_SITE) %>
+  PIWIK_LOCATION:   <%= PIWIK_LOCATION.to_json %>,
+  PIWIK_SITE:       <%= PIWIK_SITE.to_json %>,
+<% end %>
   MAX_REQUEST_AREA: <%= MAX_REQUEST_AREA.to_json %>,
   SERVER_URL:       <%= SERVER_URL.to_json %>,
   API_VERSION:      <%= API_VERSION.to_json %>
diff --git a/app/assets/javascripts/piwik.js b/app/assets/javascripts/piwik.js
new file mode 100644 (file)
index 0000000..d0443ae
--- /dev/null
@@ -0,0 +1,21 @@
+if (OSM.PIWIK_LOCATION && OSM.PIWIK_SITE) {
+  $(document).ready(function () {
+    var base = document.location.protocol + "//" + OSM.PIWIK_LOCATION + "/";
+
+    $.ajax({
+      url: base + "piwik.js",
+      dataType: "script",
+      cache: true,
+      success: function () {
+        var piwikTracker = Piwik.getTracker(base + "piwik.php", OSM.PIWIK_SITE);
+      
+        piwikTracker.trackPageView();
+        piwikTracker.enableLinkTracking();
+      
+        $("meta[name=piwik-goal]").each(function () {
+          piwikTracker.trackGoal($(this).attr("content"));
+        });
+      }
+    });
+  });
+}
index 133a746a81b0d55a34450f0c4baa15cda4b9e6c6..993544baed158495fac4c2ba05702f8d64e1d9d5 100644 (file)
@@ -11,6 +11,9 @@
   <%= tag("link", { :rel => "publisher", :href => "https://plus.google.com/111953119785824514010" }) %>
   <%= tag("link", { :rel => "search", :type => "application/opensearchdescription+xml", :title => "OpenStreetMap Search", :href => asset_path("osm.xml") }) %>
   <%= tag("meta", { :name => "description", :content => "OpenStreetMap is the free wiki world map." }) %>
+  <% if flash[:piwik_goal] -%>
+  <%= tag("meta", :name => "piwik-goal", :content => flash[:piwik_goal]) %>
+  <% end -%>  
   <%= style_rules %>
   <%= yield :head %>
   <%= csrf_meta_tag %>
diff --git a/app/views/layouts/_piwik.html.erb b/app/views/layouts/_piwik.html.erb
deleted file mode 100644 (file)
index 38db969..0000000
+++ /dev/null
@@ -1,14 +0,0 @@
-<script type="text/javascript">
-var _paq = _paq || [];
-(function(){ var u="<%= request.protocol %><%= PIWIK_LOCATION %>/";
-_paq.push(['setSiteId', <%= PIWIK_SITE %>]);
-_paq.push(['setTrackerUrl', u+'piwik.php']);
-_paq.push(['trackPageView']);
-_paq.push(['enableLinkTracking']);
-<% if flash[:piwik_goal] -%>
-_paq.push(['trackGoal', <%= flash[:piwik_goal] %>);
-<% end -%>
-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>
-<noscript><p><img src="<%= request.protocol %><%= PIWIK_LOCATION %>/piwik.php?idsite=<%= PIWIK_SITE %>" style="border:0" alt="" /></p></noscript>
index e5c7e1bdd384ed50554a3674aa78697a3b83cb43..b51191189a53d085dba8d2215497a494c938c8d5 100644 (file)
     </div>
 
     <% if defined?(PIWIK_LOCATION) and defined?(PIWIK_SITE) -%>
-    <%= render :partial => "layouts/piwik" %>
+    <noscript><p><img src="<%= request.protocol %><%= PIWIK_LOCATION %>/piwik.php?idsite=<%= PIWIK_SITE %>" style="border:0" alt="" /></p></noscript>
     <% end -%>
   </body>
 </html>