If you're into analyzing the traffic of your Joomla website (as you should be IMHO), you will probably need to add some tracking software to your site. The most obvious one is Google Analytics. Other choices can be Crazyegg, GoingUp or other solutions.
Module position basics
If you've been working with Joomla, you know that a Joomla template consists of HTML and some PHP snippets.
For one, it contains a component area where the output from the different components are shown (articles, blog lists, third-party components etc). A Joomla template also contains several PHP snippets, which are called module positions. You can assign modules to these positions and they will appear in the given position in your template.
Easier way to insert scripts
Of course, you can use an extension for each third-party script you want to enter. Probably there is one available (isn't there for almost anything?). The problem with this approach, though, is that you need to keep track of X number of extensions and their updates. And for each extension you add to your site, the chance of being hacked increases. After all, code is made by humans, and humans make mistakes.
The method is simple, but effective.
You will need three module positions, and as many Custom HTML modules as you need (one for each snippet).
Inserting the module positions in your template
In order to make this as flexible as possible, you need three module positions:
Now you're ready to insert the module positions. You will need to enter the following code into your template index.php file. You'll find it here: /templates/yourtemplatename/index.php.
Make sure to create a backup of your index.php file before messing with it.
Enter this just before the </head> tag.
<jdoc:include type="modules" name="endhead" />
Enter this just after the <body> tag:
<jdoc:include type="modules" name="topbody" />
Enter this just before the </body> tag:
<jdoc:include type="modules" name="endbody" />
Save your template.
Adding the script to the Custom HTML module
I'll use Google Analytics as an example. Go to the Module Manager and click New. Choose the 'Custom HTML' module type. You will see the module parameters.
Enter a title for the module, for instance 'Google Analytics code', enable the module, choose not to show the module title.
For the Google Analytics code, we assign the module to the newly created 'endhead' position. This has the Google Analytics snippet appear just before the closing HEAD tag (</head>). As you have not entered the new module positions to the template XML file, they will not appear in the module position drop-down. Not to worry: You can simply enter the position name 'endhead' in the field directly.
Lastly, enter the HTML mode of your editor and paste the Google Analytics code into the window. You can now save the module.
Reload your website frontend and look for the snippet.
Adding more snippets
I find this method easy to use and flexible. Hope you enjoy it and that it saves you some time and frustration