Adding Piwik Tracking Code to all pages served by Apache

Its pretty common to encounter the scenarios listed

  1. The webmaster / web site administrator has no control over the website / application code
  2. Add the tracking code to all pages of the web application is a very cumbersome process / no bandwidth with the development team for this effort.

The option listed below can be used to add the tracking code to all pages. The snippet below works for the Apache Web Server. I haven’t got a chance to find out other equivalents on IIS and other servers.

Pre-reqs

  1. Apache Web Server is already installed and website is running smoothly
  2. You have access to edit the configuration of the web server and/or you have access to the admin who does this for you :slight_smile:

Process.

  1. Compile mod_substitute and mod_ext_filter and add them as DSO modules
  2. Update the httpd.conf

LoadModule ext_filter_module modules/mod_ext_filter.so
LoadModule substitute_module modules/mod_substitute.so
...
..
AddType application/x-javascript .js
....
...

If using a virtual host section add the snippet inside the virtual host


AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|</body>|<script type=\"text/javascript\" src=\"http://analytics.mydomain.com/piwik.js\"></script><script type=\"text/javascript\">var siteid=\"XX\
";</script><script type=\"text/javascript\" src=\"http://analytics.mydomain.com/customscript.js\"></script></body>|ni"

In the above code it is assumed that the tag is present. In case its not view the source of your page and add an appropriate tag. I would recommend placing this script at the bottom of the page.

Replace siteid=“XX” with the value of the website configured via the Piwik Admin Screen.

Inside the customscript.js place the javascript tracking code


try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", siteid);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}

Check the syntax of the httpd.conf file using the configtest command


[root@myhost ~]# /<Apache_Install_Home>/bin/apachectl configtest
Syntax OK

Restart your web server , click through your application and check the Piwik Analytics Portal to see the hits coming in :slight_smile:

Limitations of this method
–> Only out of the box tracking is available. Capturing custom variable values is not be possible

Your above post is sort of confusing. My problem is that I can not locate my tracking code, where do I cacate it, and which of the above is the tracking coce a man might be able to use for Wordpress?

Hi Greg,

Once you have a fully running setup of Piwik installed and have added a website, you will be able to get the tracking code for your website by logging into the Piwik Admin Page

Go to Settings (link on right top) --> Websites tab --> Select the Website you have setup and then scroll to the right to “view the tracking code”

Hi Aye Kay

I did all your steps, but piwik don’t capture anythink.
Traking Code=Check
Sysntax OK

I don’t know how it is the problem.

Thanks

@ismaelpuerto, if you don’t provide a link or a more specific error report, we can’t help you. Please provide a link to your website’so people can take a look and make sure the tracking code is working.

Hi Fabian. The website is private (intranet)

Access_log apache Piwik:

192.168.100.15 - - [26/Mar/2013:10:47:11 +0100] “GET /piwik/piwik.js HTTP/1.1” 304 - “http://192.168.100.147/” “Opera/9.80 (X11; Linux i686) Presto/2.12.388 Version/12.14”

Access_log apache Client Piwik

192.168.100.15 - - [26/Mar/2013:10:50:07 +0100] “GET /customscript.js HTTP/1.1” 304 - “http://192.168.100.147/” “Opera/9.80 (X11; Linux i686) Presto/2.12.388 Version/12.14”

But, in piwik don’t register anythink

hi,

i’m tracking all the pages with this method, and would like to know if it is possible to exclude a path. thanks in advance.

Hi
I’m getting the javascript code added twice to my sites using this method. Any idea why this should be?

Example site www.wowmums.org.uk

extract from VirtualHosts


</Directory>
AddOutputFilterByType SUBSTITUTE text/html
Substitute "s|</body>|<script type=\"text/javascript\" src=\"http://analytics.2020media.net.uk/piwik.js\"></script><script type=\"text/javascript\">var siteid=\"4\";</script><script type=\"text/javascript\" src=\"http://analytics.2020media.net.uk/customscript.js\"></script></body>|ni"
</VirtualHost>

Hi ,

I am also trying to place tracking code on apache i am getting log on both apache server piwik server but in piwik don’t register any thing.

apache - server
X.X.X.X - - [24/Dec/2014:18:01:35 +0530] “GET /favicon.ico HTTP/1.1” 404 582 “-” “Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36”

piwik - server
X.X.X.X - - [24/Dec/2014:12:31:33 +0530] “GET /piwik/piwik.js HTTP/1.1” 200 42605 “http://xxxxx.com/” “Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36”

My apache config

AddOutputFilterByType SUBSTITUTE text/html
Substitute “s||<script type=“text/javascript” src=“http://X.X.X.X/piwik/piwik.js”><script type=“text/javascript”>var siteid=“2”;<script type=“text/javascript” src=“http://X.X.X.X/piwik/customscript.js”>|ni”

customscript.js

try {
var piwikTracker = Piwik.getTracker(pkBaseURL + “piwik.php”, siteid);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}

Although this method does work, I have found sometimes I get javascript conflicts resulting in problems for the sites that are using it.

Regrettably therefore I have decided to abandon it and install Piwik on each site.

Thought I would offer another option for those looking. I have had pretty good success injecting a php script into my index.html files as Apache is serving them. A simple FilesMatch directive does the trick for me. Another perk of this approach is that it gives me the freedom to use global server variables in the tracking script for user identification and the like. My tracking.php is just the standard javascript with a few php phrases and if() statements to ensure I only pass those variables to Piwik if a user has logged in.

Note: this approach requires libphp5.so (php5). I keep the code listed below in a virtual host config file, but that’s just my preference as my setup keeps all my config in virtualhost config files, rather than using .htaccess files.


<FilesMatch "^(index|home)\.html$">
        AddHandler php5-script .html
        php_value auto_append_file /var/www/tracking.php
        php_flag display_errors Off
</FilesMatch>

Have a nice day!

Based on this solution I came up with one that injects the Piwik tracking javascript:

<LocationMatch "/">
  AddOutputFilterByType SUBSTITUTE text/html
  Substitute "s-</head>-<script type=\"text/javascript\">var _paq = _paq || [];_paq.push(['trackPageView']);_paq.push(['enableLinkTracking']);(function() {var u=\"//example.org/piwik/\";_paq\
.push(['setTrackerUrl', u+'piwik.php']);_paq.push(['setSiteId', '3']);var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0];g.type='text/javascript'; g.async=true; g.defer=true; g.s\
rc=u+'piwik.js'; s.parentNode.insertBefore(g,s);})();</script></head>-n"
</LocationMatch>

Make sure to replace example.org and set site id with your actual value.