来源:http://drupal.org/node/171205
I've opted for the third method. First I created a file called external-js.tpl.php in my theme directory, containing this:
<?php <script type="text/javascript" src="http://www.google-analytics.com/urchin.js"></script> ?>
Then, in my template.php file, I created a pre-process function that
loads that into a variable and adds the appropriate JS function call to
$footer:
<?php function Nefertari_preprocess_page(&$vars){ $path = drupal_get_path('theme', 'Nefertari'); $vars['external_js'] = theme_render_template($path.'/external-js.tpl.php', $vars); $vars['footer'] .= '<script type="text/javascript"> _uacct = "UA-GOOGLE-ID"; urchinTracker(); </script>'; } ?>
Lastly, I modified page.tpl.php to print the variable $external_js
right after $script in the header. I could also have printed it right
before the footer, and that might have been better for performance.
There's actually a module for Google Analytics already, but it doesn't
fit my needs very smoothly (it's designed for single sites, when I'm
working with a multi-site install of nearly a hundred distinct Drupals). |