Skip to main content
deleted 55 characters in body
Source Link

The answer by Muhammad Reda is actually not very good: On a Drupal site, you want to insert additional javascript using the drupal_add_js function inside the THEME_preprocess_html function in the template.php file. Doing so allows you to properly cache your site. Specifically, this is how it would look:

<?php function THEME_process_pageTHEME_preprocess_html(&$variables) { $ga = "var _gaq = _gaq || [];\n"; $ga .= "_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);\n"; $ga .= "_gaq.push(['_setDomainName', 'example.com']);\n"; $ga .= "_gaq.push(['_trackPageview']);\n"; $ga .= "(function() {\n"; $ga .= " var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n"; $ga .= " ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n"; $ga .= " var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n"; $ga .= "})();\n"; drupal_add_js($ga, array('type' => 'inline', 'scope' => 'header')); } ?> 

Be sure to replace the UA ID and website name with your own. Also, be sure to rename THEME to your theme and clear the cache when complete.

The answer by Muhammad Reda is actually not very good: On a Drupal site, you want to insert additional javascript using the drupal_add_js function inside the THEME_preprocess_html function in the template.php file. Doing so allows you to properly cache your site. Specifically, this is how it would look:

<?php function THEME_process_page(&$variables) { $ga = "var _gaq = _gaq || [];\n"; $ga .= "_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);\n"; $ga .= "_gaq.push(['_setDomainName', 'example.com']);\n"; $ga .= "_gaq.push(['_trackPageview']);\n"; $ga .= "(function() {\n"; $ga .= " var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n"; $ga .= " ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n"; $ga .= " var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n"; $ga .= "})();\n"; drupal_add_js($ga, array('type' => 'inline', 'scope' => 'header')); } ?> 

Be sure to replace the UA ID and website name with your own. Also, be sure to rename THEME to your theme and clear the cache when complete.

On a Drupal site, you want to insert additional javascript using the drupal_add_js function inside the THEME_preprocess_html function in the template.php file. Doing so allows you to properly cache your site. Specifically, this is how it would look:

<?php function THEME_preprocess_html(&$variables) { $ga = "var _gaq = _gaq || [];\n"; $ga .= "_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);\n"; $ga .= "_gaq.push(['_setDomainName', 'example.com']);\n"; $ga .= "_gaq.push(['_trackPageview']);\n"; $ga .= "(function() {\n"; $ga .= " var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n"; $ga .= " ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n"; $ga .= " var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n"; $ga .= "})();\n"; drupal_add_js($ga, array('type' => 'inline', 'scope' => 'header')); } ?> 

Be sure to replace the UA ID and website name with your own. Also, be sure to rename THEME to your theme and clear the cache when complete.

Source Link

The answer by Muhammad Reda is actually not very good: On a Drupal site, you want to insert additional javascript using the drupal_add_js function inside the THEME_preprocess_html function in the template.php file. Doing so allows you to properly cache your site. Specifically, this is how it would look:

<?php function THEME_process_page(&$variables) { $ga = "var _gaq = _gaq || [];\n"; $ga .= "_gaq.push(['_setAccount', 'UA-XXXXXXX-X']);\n"; $ga .= "_gaq.push(['_setDomainName', 'example.com']);\n"; $ga .= "_gaq.push(['_trackPageview']);\n"; $ga .= "(function() {\n"; $ga .= " var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n"; $ga .= " ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n"; $ga .= " var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n"; $ga .= "})();\n"; drupal_add_js($ga, array('type' => 'inline', 'scope' => 'header')); } ?> 

Be sure to replace the UA ID and website name with your own. Also, be sure to rename THEME to your theme and clear the cache when complete.