Shall I install google analytics module or just put GA code in page.tpl file as there are too many modules and I dont want to add one more module. what do you suggest?

  • If Drupal 6, you would use page.tpl.php; Drupal 7 would be html.tpl.php... – mpdonadio Feb 17 '13 at 16:31

It is fine if you want to put the snippet in the page.tpl.php file. You will basically benefit Google Analytics module's conditional-snippet features.

If you put the snippet in page.tpl.php file,

  • Admin theme will not be tracked (which is usually Seven, which is in core - don't hack core!)

  • Users cannot opt-put for tracking (See your drupal.org account edit page for an example). this is independent from browser's Do Not Track (DNT) header.

  • You will find it difficult to track custom variables.

  • You will find it difficult to track events (downloads, etc). But you can put a modified static snippet and it should work as if it was from Google Analytics module. You can also track messages ($messages) using GA module.

  • You will need to edit all your page.tpl.php variants to add the code.

  • Other modules will not be able to control your snippet. Not compatible with js_alters.

  • You will find it difficult to track internal search.

  • All users, including admin roles, which is usually not necessary to track.

  • Cache the code locally.

In my opinion (and some little home-done tests), caching the code locally is not a good idea. GA service is so popular so the chance of having a locally cached version of ga.js is very good. If you cache it, browser will have to download the same code from your server (But you can track users those who have blocked ga.js using AdBlock plugins).

For sites that I maintain in long run, I'm fine to use the snippet directly (in page.tpl.php or html.tpl.php). Many sites don't require A/B testing, goal tracking, custom variables and other bells and whistles. If you explicitly call node_load() , user_load(), user_access(), or other functions, that's a signal that you might want to use GA module instead of the in-house coding.

  • tldr: use the module – cdmo Feb 17 '13 at 21:22

It is fine to not use the module, but the right way to do this is to put the tracking code in a js file and use drupal_add_js() or even better, drupal_add_library(). This way the code can be cached and is not reloaded on each page.

  • 1
    This is, in my opinion, wrong. Most likely that users have the ga.js code cached already, and we do need to put the content on every page load. drupal_add_js has nothing to do with performance in this case except it can add extra work to the server. – AyeshK Feb 17 '13 at 16:09

When making this decision, it might be a good idea to review http://drupal.org/node/1788586 and http://productforums.google.com/forum/#!category-topic/analytics/report-bugs/djSplZp1Crg , as the google_analytics module cannot put the js in the (odd) location that Google requires it. This is a limitation of drupal_add_js, so it won't really help you to write your own version of the ga module with your own call to drupal_add_js. Note that a workaround exists in ga; if you put the code at the very end of the page (Google's old location), then Google will pick it up. Before I discovered this workaround, I just pasted the required Javascript into my theme template file, and that has been working pretty well for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.