Any PHP / Javascript coders out there?

djs

Suspended / Banned
Messages
727
Name
Daniel
Edit My Images
Yes
Hi all,

I have a website that's done in PHP. And I would like to be able to use Piwik for analytics.
To do this, I'm required to insert a little chunk of Javascript to a page.

So what I'm wanting to do is add the chunk of JavaScript to my index.php file.

I did have it working, but it caused a few problems, so I'm wondering what is the best way to go about it.
Any pointers would be great! I've been faffing with it for ages, trying to pick up tips from Google.

Here's the contents of my index.php file:

<?php
include "fennel-index.php"; ?>



and here's the chunk of JavaScript that I want to insert into the PHP:


<!-- Piwik -->
<script type="text/javascript">
var pkBaseURL = (("https:" == document.location.protocol) ? "https://ds-photo.co.uk/piwik/" : "http://ds-photo.co.uk/piwik/");
document.write(unescape("%3Cscript src='" + pkBaseURL + "piwik.js' type='text/javascript'%3E%3C/script%3E"));
</script><script type="text/javascript">
try {
var piwikTracker = Piwik.getTracker(pkBaseURL + "piwik.php", 1);
piwikTracker.trackPageView();
piwikTracker.enableLinkTracking();
} catch( err ) {}
</script>
<noscript><p><img src="http://ds-photo.co.uk/piwik/piwik.php?idsite=1" style="border:0" alt="" /></p></noscript>
<!-- End Piwik Tracking Code -->



Thanks in advance!!
 
You need to make sure that the javascript is outside the <?php.........?> section as you dont want it parsed by the php engine.

Also find out where on the page you need it to be inserted. I would suggest putting it into the fennel-index.php file not the index.php file.
 
Thanks for the reply Ben!

Thing is, if I put the Javascript before the php code then Piwik works, but it messes with certain functions of the website.

Or, If I put the JavaScript after the php code, then it doesnt seem to get read. Only the php will get read.

The same goes if I put it in the fennel-index.php file too.

Unfortunately, I don't know JavaScript or PHP. I can just about read JavaScript and understand what it's doing, but that's about it.
 
if the index page says and only says

<?php
include "fennel-index.php"; ?>

then you need to add the piwik code to the fennel-index.php file

Thats an odd structure though

If your structure is more along the lines of

<?php include "header.php"; ?>
<?php include "fennel-index.php"; ?>
<?php include "footer.php"; ?>


Then if you include the javascript in the footer file, then presumably, it will then be added to all of the pages in the site

As piwik is a tracker, you want it loading after everything else

so whatever file you end up having to drop it in look for the bit that says

</html>

...and drop it in just before that
 
Hi Richard, thanks for your reply and time!

Yeh, that's the only bit of code in the index.php file

So, index.php calls fennel-index.php.
fennel-index.php also contains only the following code:

<?php
$namespace = implode("-", array_slice(explode("-", basename(__FILE__, ".php")), 0, -1)); include "$namespace-config.php"; include $corepath."/php/render.page.php"; ?>


Unfortunately, there's no footer file :(

The more I look into this, the more confused I get :thinking:
 
Well, I think it's time for me to give up on this one.

I've been in contact with the template writers, and they said "unfortunately, our sites don't support anything but google analytics because of a lack of dynamic api."

Oh well...
 
Are you using a content management system like Wordpress?
 
Hiya,

My blog is wordpress, but my main site isn't. It's from Intothedarkroom.
It lets you use Google Analytics, but I hate it. It's nowhere near as good as Piwik.
 
Back
Top