Google Analytics

If you are a user of Google Analytics, you may want to use the service with one of your Textpattern installations.

Create a form called google_analytics

Assuming you already have a Google Analytics account, login and get the Analytics code for your site.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("your user account id");
pageTracker._trackPageview();
</script>

Enter the above into your new form, and make sure to add your user account id. Save as type misc.

Place a call to Google Analytics in your page template

Just before the closing </body> tag, place the following:

<!-- google analytics -->
<txp:output_form form="google_analytics" />

Once this is done, you should be able up and running. Check your Google Analytics account for reports and options.

4 Comments - Comments RSS Feed

Olhado
# 20 November 2008

It’s a “TxP tip”? :)

Jonathan Stubbs
# 23 November 2008

Yep, its a tip. There are many people using Textpattern who look for ways to achieve what they need, and sometimes those needs are simple. Not every tip is for advanced users. ;-)

Michael W
# 23 December 2008

Why would you not just put the JS in the template? Why embed it in a form tag? Does this help you pass variables into the GA system or some other benefit I am missing?

Jonathan Stubbs
# 23 December 2008

You can place the JS in a page template, but if you have many pages, output_form saves some work if you ever have to update the Google JS.

Add a comment

Required

Required, not displayed

Optional

Use Textile Help to style your comments