Add Google Analytic Code to WordPress Footer With add_action function

Simple snippet to add script to your WordPress template footer without having to add it into your footer.php. You can use this to append Google Analytic Code by using add_action function and append your script to wp_footer. It's a cleaner solution so your html file always separate from Javascript.


Open functions.php and append the following. Of course, change $account to your google analytic account.

add_action('wp_footer', 'ga');

function ga() {

	$account = 'UA-1234567-8'; // your account id should look something like this.
	$code = "<script type=\"text/javascript\"> 
	  var _gaq = _gaq || [];
	  _gaq.push(['_setAccount', '$account']);
	  (function() {
	    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '';
	    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	echo $code;


Share the love

Leave a Comment

Please keep in mind that comments are moderated and rel="nofollow" is in use. You can use [code][/code] if you want to write codes. Don't spam us :) Thanks!