Textpattern tips, tutorials and code snippets

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.

8 Comments Comment feed

It’s a “TxP tip”? :)

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. ;-)

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?

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.

  • Tim
  • 14 September 2012

Thanks for this! I’m helping a friend to integrate Analytics and am unfamiliar with TXP. Will I need to add

<!— google analytics —>
<txp:output_form form=“google_analytics” />

to the bottom of every page template, or will adding to the default page overarch the entire website?

Appreciate the help!

Tim - the first line <!— google analytics —> is just an HTML comment and its up to you whether you put this in, its certainly not needed.

The call to the form <txp:output_form form="google_analytics" /> should be placed on all active pages, meaning any page template which is used by TXP.

  • Michael
  • 29 November 2012

I am trying to figure out how to tag multiple links on a single page. I have over 100 of them that i want to track – do I just have to include
<txp:output_form form=“google_analytics” /> in the <a> script for each link?

I want to make sure i am doing it right before I add all – i know this is a pretty basic question but i appreciate any help!

@Michael – not quite sure what you mean by tag multiple links, but Google will track everything so long as you place the code on each page template you have.

Add a comment

Use Textile help to style your comments