Custom Joomla module for Google Analytics

This guide applies to:
Joomla 1.5.X

How to Create a custom module for Google Analytics in Joomla 1.5

Define the new module position

Open your template XML configuration file (/templates/<templatename>/templateDetails.xml). Locate the <positions> tag in your XML file. It looks something like this:

<positions>
      <position>left</position>
      <position>right</position>
      <position>user1</position>
</positions>

Add your own position (googleanalytics). The end result will be something like this:

<positions>
      <position>left</position>
      <position>right</position>
      <position>user1</position>
      <position>googleanalytics</position>
</positions>

Save the changes.

Create the position

Navigate to 'Extensions > Template Manager' and click on your default template name. In the 'Template: [ Edit ]' view, click on the 'Edit HTML' button. Now you see the PHP code for your tempate.

Locate the </head> tag and insert the position code (<jdoc:include type="modules" name="googleanalytics" />) immediately before it. The end result will be something like this:

...
<jdoc:include type="modules" name="googleanalytics" />
</head>

Save the changes.

Create the module

Before you create the module, go to Site > Global Configuration and change the default WYSIWYG editor to 'No editor' (don't forget to change it back when you are done).

Go to Extensions > Module Manager and click on the 'New' button. Select 'Custom HTML' as the module type and click 'Next'.

In the details section, change the module settings to the following:

Title: Google Analytics
Show Title: No
Position: googleanalytics

Insert your Google Analytics Tracking Code (urchin.js or ga.js) in the 'Custom Output' field. It will look something like this:

<script type="text/javascript">
  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-12345678-1']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();
</script>

Click on 'Save'.

Verify

Use this guide at Google Analytics Help to verify: https://www.google.com/support/googleanalytics/bin/answer.py?answer=55480&hl=en_US

Tags: 

Theme by Danetsoft and Danang Probo Sayekti inspired by Maksimer