Welcome to WebmasterWorld Guest from 184.73.13.66

Forum Moderators: open

Message Too Old, No Replies

Changing the Piwik Javascript snippet

   
8:29 pm on Mar 11, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not familiar with Javascript but I tried to change the Piwik Javascript snippet to use all relative URLs. Does this look OK?

<!-- Piwik -->
<script type="text/javascript">
document.write(unescape("%3Cscript src='/piwik/piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker("/piwik/piwik.php", 1);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}
</script><noscript><p><img src="/piwik/piwik.php?idsite=1" style="border:0" alt="" /></p></noscript>
<!-- End Piwik Tracking Code -->
10:47 pm on Mar 11, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



When you say "relative" do you mean "site-absolute"?

fwiw, mine says:

IN HEAD
<script type = "text/javascript" src = "/piwik/piwik.js"></script>

AT END
<!-- Piwik -->
<script type = "text/javascript">
try
{
var piwikTracker = Piwik.getTracker("http://www.example.com/piwik/piwik.php", 1);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
}
catch( err ) {}
</script>

<noscript>
<p><img src = "/piwik/piwik.php?idsite=1" style = "border: 0" alt = ""></p>
</noscript>


I only kept the http://www.example.com piece because I use it on two different sites, so it's enough work just remembering to change 1 to 2 ;) .
3:49 pm on Mar 12, 2012 (gmt 0)

WebmasterWorld Senior Member rocknbil is a WebmasterWorld Top Contributor of All Time 10+ Year Member



If those Javascript and PHP scripts are actually on your site, it is absolutely fine and will work from any directory, over SSL or not, both will work. That is, if this code is in a common footer, and you move to a secure area, /this/that.js will still be at https.... UNLESS you have your secure content housed in a different directory. Which most don't.
4:47 pm on Mar 12, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



The default piwik script starts with

<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://www.example.com/piwik/" : "http://www.example.com/piwik/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script>

so the question here is about trimming it down to work on a specific site under specific circumstances. At least I hope that's what the question was.
5:02 pm on Mar 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



lucy24, yes, is my rearrangement of the code OK? I changed it around and I'm not sure if it is OK since I haven't used Javascript before.

What's up with that snippet in the HEAD? piwik.org didn't present that to me. Maybe it isn't necessary anymore?
5:41 pm on Mar 13, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



I wouldn't put it in the head. The best place for scripts is just before the closing </body> tag.

Tonearm, you code can be further simplified. The piece of code that piwik generates does the following:
1. Determines the pkBaseURL (an absolute URL) based on the current protocol (http or https)
2. Uses document.write to output the script tag pointing to pkBaseURL using that same protocol.

If you're pointing to a relative URL (including relative to the site root) then you don't need to determine the current protocol because it will use the same protocol as the current document. Therefore, you don't need to worry about either steps 1 or 2 above, and you could change your code to:

<!-- Piwik -->
<script type="text/javascript" src="/piwik/piwik.js"></script>
<script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker("/piwik/piwik.php", 1);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}
</script><noscript><p><img src="/piwik/piwik.php?idsite=1" style="border:0" alt="" /></p></noscript>
<!-- End Piwik Tracking Code -->

Placing that entire bit just before the closing </body> tag.
6:31 pm on Mar 13, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks Fotiman, good stuff!
6:49 pm on Mar 13, 2012 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



What's up with that snippet in the HEAD?

It's a side effect of changing from a generic script to a specific one. There are three parts:

#1 a snippet on each page, along with a pointer that tells you where to find
#2 a little script that lives in /piwik/piwik.js on each separate site, which in turn calls
#3 the "real" piwik, the .php part, which lives in a single location on a single site.

Fotiman, the script itself isn't in the head; that's just the pointer to where to find it. Earlier versions of piwik had you put script #2 -- it was about 20-30 lines* -- at the bottom of each page, unless you were brave enough to split it manually into parts 1 and 2.


* It's now more like 50-100 but hard to count because it's much more heavily commented.
 

Featured Threads

Hot Threads This Week

Hot Threads This Month