I have a website made up of only HTML pages. Instead of copy pasting the Google Analytics code on every .html file, is there a better way to include the Google code on every page?


I see everyone recommending a server side solution for this, which is fine, but if you're going to have to go through all of their files and append a server side include into your pages why not just put the Google Analytics code into a separate file and include it using a <script> tag? That way there is no server side processing required which is a performance gain.

<script type="text/javascript" src="/javascripts/ga.js"></script>

  • 2
    +1 Yep this is definitely the right best answer!!! There is also a SO question on this: stackoverflow.com/questions/1131079/… Jul 28 '11 at 13:39
  • I hadn't considered this, but I would have thought the blocking nature of Javascript would have made this less efficient than SSI? Would need to run tests top confirm of course.
    – Toby
    Jul 28 '11 at 16:15
  • 1
    Yep, you'll lose the async goodness if you have the script in an external file: stackoverflow.com/questions/3263818/…
    – Ciaran
    Jul 28 '11 at 17:14
  • @Ciaran: From the link you posted: what if the _gaq variable is globally scoped ? Will the async analytics still be affected ? Jul 28 '11 at 19:43
  • 1
    I think the analytics should work, but it won't load asynchronously. External javascript files are blocking: code.google.com/speed/articles/include-scripts-properly.html
    – Ciaran
    Jul 29 '11 at 1:23

Server Side Includes could be your way forward.


They are generally ignored because scripting languages like PHP do a better job, but these will get the job done for you.

  • +1 Great link, thanks for sharing, I'm used to look at Apache docs, but the Wikipedia page is done really well. Jul 28 '11 at 12:37


using Toby suggestions this is the code to put in ALL your HTML pages just before the </head> closing tag (specific for Google Anaytics):

<!--#include virtual="footer.html" --> <!-- trailing space char is not a typo -->

Then you create a file called footer.html where you just write into it the code provided by Google Analytics, and nothing else i.e.:

<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-...']);

With PHP it works in the same way, simply add this PHP code in ALL your HTML pages just before the </head> closing tag:

<?php include("footer.html"); ?>    
  • @brainydexter: John Conde answer is the simple right thing to do, with no need of server side scripting nor ssl. I live my answer here just for general knowledge. Jul 28 '11 at 13:40

I just added the code above the <head> tag in my template HTML page that I made in Dreamweaver so it will be on all of my pages. This seems like the best way to me. I am not an expert so hopefully if this is wrong someone will call me on it.

  • 1
    That should work fine.
    – dan
    Jan 15 '14 at 7:05

It's been a really long time since I've used a WYSIWYG editor (probably 10 years) but I seem to recall Dreamweaver (and perhaps others) allowed you to add "template" items which would populate to every page listed in the current site. If you're using an editor such as this I'd look through their documentation or ask in their support forums first.


I think the quickest is to make footer.php for example and add the code there. After that or before that footer have to appear as element in every page of your website.

  • I have simple html pages, no php :( Jul 28 '11 at 6:08
  • @brainydexter: why don't you move you site on to an hosting with PHP, i.e. qualityhostonline.com it would cost you 3.95 bucks / month. I think they also take care to help you moving your site for free on to their servers. I never tried how they help moving the site, because I can move the sites on my own. Jul 28 '11 at 11:31

The down side to the suggestions so far is that they all require visiting each html page to add code. Sounds to me like the short answer is that you DO have to touch every page.

Ok, maybe I am being overly simplistic, but what about using a find & replace utility? Chances are that whatever software you use has find & replace functionality, and you can add something to every page quickly. For instance, if you want it to be the last thing before the tag, just find that tag and replace it with the code you wish to insert plus the tag.

Placing the GA code in a js file or a ssi file might be a big help down the road for any changes you need to make. That way you would only have to touch one file instead of revisiting all of them.


If it's a Linux server, put the google code in a local js file like the top answer said, then say something like:

    STR1='<head>'; STR2='<head><script type="text/javascript" src="/javascripts/ga.js"></script>';  replace "${STR1}" "${STR2}" -- *.html

I just tried on Ubuntu Server 12.04 and it worked. I'd try it on one file first to be sure before using the wildcard.

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.