Category Archives: Coding

Adding Analytics to WordPress

This is somewhat for my own reference, but if anyone else on the course (or wider internet) wants to know how to manually add Google Analytics to your WordPress site. Now whilst there are widgets to do this, as our resident code guru Kyle says, it’s good to get an idea of how it all works (Shoutout to my main man Kyle, if you’re somehow reading this). So, this being my first tutorial on here, lets give it a go:

  • Read this sentence.
  • Sign up for Google Analytics, it should the give you a code which looks like this:

  • Download Filezilla – a program which links your computer to the virtual one your server is hosted on – think of it as a slightly more complicated flash drive. Open it up and you’ll want to focus on this top bar for the moment:

  • Here’s what you type in for each box –

Host: sftp://YOUR_WEBSITES_IP  (e.g. 138.68.174.34, making sure you don’t include the http://)

Username: root

Password: (Your website’s password)

Port: 22 (I don’t know why, I’m just following orders)

  • If you successfully fill this in, your client should look something like this:

  • Now, you’ve got two sections – ‘Local site’ is your computer and ‘Remote site’ is your server’s virtual one. If you’ve got this far, you can now treat it like a normal flash drive – just drag and drop between them. Now it’s time to add the analytics code.
  • On the remote site, click the little minus next to the folder at the very top – this will bring you up and out of all the folders to the main one. From there you need to go through these folders:
  • var→www→html→wp-content→themes – once in the ‘themes’ folder, find the folder with the name for the theme you are currently using on WordPress and open it up.
  • Inside it there will be a file called ‘footer.php’ which you need to click and drag into you Local site (make sure you know where it will be saved there, by selecting a relevant folder in Local site).
  • Open this up (I use Brackets but you can use whatever coding software you like) and scroll to the bottom, where you’re gonna paste your analytics code (Above the </body> line). If you’ve done it correctly, it should look something like this:

  • Now save the file and click+drag it back into Remote Site – boom, you’ve got yourself some analytics.
  • Yes I did just use boom unironically.
  • And there you go, you should be able to now go on Google Analytics and see how/when people are using your site.

Side note:

If you want to change it so that you don’t affect the analytics when you visit to post stuff, you just need to add these lines of code above and below your analytics stuff:

Above – <?php if(!is_user_logged_in()): ?>

Below – <?php endif; ?>

Like so: