Return to Snippet

Revision: 17172
at August 26, 2009 01:09 by brettbergeron


Initial Code
<?php

	#
	#	Google Analytics Code
	#				

		if ( ! function_exists('google_analytics'))
		{
			function google_analytics($account)
			{
				$code  = "<script src='http://www.google-analytics.com/ga.js' type='text/javascript'>";
				$code .= '<script type="text/javascript">';
				$code .= ' try{ ';
				$code .= ' var pageTracker = _gat._getTracker("'.$account.'"); ';
				$code .= ' pageTracker._trackPageview(); ';
				$code .= ' } catch(err) {} ';
				$code .= '</script>';
				
				return $code;
			}
		}
		
	#
	#	Event Tracking
	#				

		if ( ! function_exists('track_event'))
		{
			function track_event($category, $action, $label = '', $value = '')
			{
				$code  = 'pageTracker._trackEvent(';
				$code .= "category = '$category' ";
				$code .= "action = '$action' ";
				
				$code .= ( $label == '' ) ? '' : "label ='$label'";
				$code .= ( $value == '' ) ? '' : "value ='$value'";
				
				$code .= ');';
				
				return $code;
			}
		}

Initial URL
http://brettbergeron.com

Initial Description
Below are two functions I've been using to make event tracking a little easier in CodeIgniter.

Two functions are available, google_analytics() & track_event().

google_analytics() takes one argument and uses it as the UA code for your tracking profile. track_event has two required parameters, $category and $event. The other two items are optional and won't be inserted unless specified.

Usage is pretty straight-forward:

<?=>

<?=>
<?=>

Initial Title
Google Analytics Helper for CodeIgniter

Initial Tags
php, google, codeigniter

Initial Language
PHP