How to Add Google Analytics to Your Joomla Site in a Future Proof Way!

Standard

If you are a website owner then I need not explain the importance of integrating Google Analytics in a website. I would go to the extent of saying that if there is a website, it has to include Google Analytics tracking code because that’s the best analytics tool ever and is available to everyone free of cost.

Adding the tracking code is easy, you just need to copy paste a few lines of Javascript in your web pages. And if you use Joomla then you would find a lot of extensions which gives you an easy way to add the tracking code to your Joomla site. Another way is to copy paste the tracking code in your Joomla website’s active template (frontend) but this approach has a downside. When, and if, you upgrade the template in future, your tracking code will be lost and you would need to add it again.

In the two approaches mentioned above, adding the tracking code via extensions is clearly better since you need not worry if upgrading template might result in loss/overwriting of your tracking code. But even with this approach, there are few issues mentioned below.

  1. A good many number of (specially free) Joomla extensions are one-off efforts by individual developers where they create it, make it available for anyone to use and then forget about it. Such extensions are rarely, if at all, upgraded to keep up with Joomla upgrades. At some point, such extensions fail to work because they no longer support the latest Joomla version which results in Google Analytics not working on your website.
  2. Most of the Joomla extensions for Google Analytics integration typically require your tracking ID only and hardcode rest of the tracking code within their extension. Now if Google makes any changes to their tracking code then the extension would stop working unless,
    • extension developer updates the extension,
    • you uninstall the old version and install the latest version.

    Google doesn’t update such things very often but they did that when they replaced the (now legacy) ga.js tracking code with analytics.js. In short, it’s always a possibility and you would want to be on the safer side.

  3. As mentioned in previous point, such extensions generally require only tracking ID, which looks something like UA-1234567-89, and hardcode rest of the tracking code in their extension using default tracking code. tracking idThough most of the webmasters would be happy with default tracking settings/code, Google Analytics allows you to customize your tracking settings. For example, you can track the activities of individual users by including user identifier in their tracking code and such changes may require additional code which is most likely missing in these extensions. And if you would like to benefit from such capabilities of Google Analytics then you might find yourself in trouble if you rely on such Joomla extensions.

One possible option could be to use a commercial extension which provides you all the features and customization flexibility for Google Analytics integration with good support and roadmap for upgrades/updates. But when Google Analytics itself is free (not talking about the Enterprise version folks), why would you want to pay for an extension to just integrate it in your website?

So what’s the cleanest and future-proof way to integrate Google Analytics in your Joomla website which also doesn’t cost anything?

Here is what I do if I need to integrate Google Analytics in a Joomla site.

  1. Download Sourcerer (free version) from here and install on your Joomla site. Sourcerer allows Joomla administrators to insert code (Javascript, CSS and even PHP) within the editor wherever they want, e.g. inside an article or in a module. It’s a very handy extension and can be used under a variety of situations. You can also use any other extension which provides this capability but I would choose this one over others any day.
  2. Make sure that Sourcerer plugin is enabled.sourcerer plugin
  3. Now go to Module Manager and create a new Custom type module.custom module
  4. From your Google Analytics account, copy the whole tracking code and paste it in the module’s editor. Now surround the pasted code within {source} and {/source} tags as shown below. These tags lets Sourcerer know that editor shouldn’t strips the Javascript code while saving.ga code
  5. Give any name to the module you like, select Hide option for “Show Title” and “Position” as debug as shown in the image above.
  6. Change to Menu Assignment tab and select “On all pages” as shown below.show on all pages
  7. Save the module and you’re done. Open your website in any browser and view the page source to verify that analytics code that you added is present. If you aren’t sure how to do that, you can just wait for sometime and check your Google Analytics data for same day. It should show at least one visit (you) assuming you visit the website at least once after making all the changes.

Let me quickly try to explain what we did above. Most of the Joomla templates have a debug position which isn’t visible on the website and can be used for a variety of things. Using it for Google Analytics tracking code is a classic example of how you can use it. Using the debug position and ability of Sourcerer to add Javascript code in editor, we added the tracking code in a custom module and published it on all pages. Since the module is enabled on debug position, it’s not visible to end users but code is added to the HTML rendered by Joomla template.

With this approach you’re in full control of the Google Analytics code and it doesn’t require any more efforts than any of the two approaches (adding tracking code in template or using an extension). Further, it’s future proof from Joomla upgrades and template upgrades since custom module type is a native Joomla module. If you ever make any customization to your tracking code or if Google asks you to upgrade your tracking code, all you need to do it copy and replace the tracking code in the module that we created.

The only dependency that we have here is on Sourcerer but I have known that extension since I know Joomla (more than 7 years) and it has been free since then. I don’t see any reason why it would be made paid or discontinued if that hasn’t happened so far.

So now you know the best way to add Google Analytics tracking code to your Joomla website 🙂

And yes, if you ever need website development services for Joomla, e.g. new Joomla website, custom Joomla component development etc, then you can use our Joomla web design services.

Leave a Reply