When a Joomla developer is asked to add some JavaScript tracking code to a Joomla website, he typically codes a content plugin in order to achieve this result. This, of course, works. But, it’s a very inefficient way of doing things. Why? Because this can be done easily with a custom HTML module and a layout override. Here’s how:
- Open the index.php file under the templates/[your-joomla-template] folder.
-
Just before the closing body tag (e.g. just before </body>) add the following code:
<jdoc:include type="modules" name="code-bottom" style="raw" />
With the above line, we have added a new position to the template where we can add our JavaScript code.
-
Create a nodiv layout override for the Custom (previously called Custom HTML) module. You can find the full details on how to do so here.
-
Create a new Custom module, and assign it to the code-bottom position.
-
Add your JavaScript to the module that you’ve just created, and then click on the Advanced tab, and finally choose nodiv from the Layout dropdown.
-
Save the module and clear your Joomla cache.
-
That’s it! Now you will have your tracking code on all pages (you can even easily choose which pages to exclude/include since you are using a module).
But what if the JavaScript code is slightly dynamic?
Well, in this case, you can create a specific module override (instead of the nodiv module override) and then add your logic there.
So, are there real advantages over creating a plugin?
Definitely! First, the more plugins you have installed and activated on your Joomla website, the slower it is. Second, we’re leveraging an existing extension to do what we want (reusability), and third, once the module position and the layout override are in place, a Joomla administrator with no coding experience will be able to add any tracking code to the website.
We hope that you found our short post useful. If you need help with the implementation, then you can always contact us. We are always ready to help, we love working with new clients (and, of course, existing clients), and our rates are very affordable.
Hi
I love this custom mod without div you posted, especially for adding FB Pixel to specific pages.
I wish I found it long ago.
I am now trying to move sites over to Joomla 4, and created the module without the tag, as you describe above,
but am struggling to input the .
I have a “
" or a "" tag around it, and the content displays on the page, instead of running behind.
Have you managed to adapt this method to work in Joomla 4 successfully?
Thank you