homepage Welcome to WebmasterWorld Guest from 54.205.119.163
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Changing the Piwik Javascript snippet
Tonearm




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

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 -->

 

lucy24




msg:4427942
 10:47 pm on Mar 11, 2012 (gmt 0)

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 ;) .

rocknbil




msg:4428157
 3:49 pm on Mar 12, 2012 (gmt 0)

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.

lucy24




msg:4428180
 4:47 pm on Mar 12, 2012 (gmt 0)

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.

Tonearm




msg:4428677
 5:02 pm on Mar 13, 2012 (gmt 0)

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?

Fotiman




msg:4428690
 5:41 pm on Mar 13, 2012 (gmt 0)

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.

Tonearm




msg:4428724
 6:31 pm on Mar 13, 2012 (gmt 0)

Thanks Fotiman, good stuff!

lucy24




msg:4428731
 6:49 pm on Mar 13, 2012 (gmt 0)

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved