: Easier way to include Google Analytics script code on every page? I have a website made up of only HTML pages. Instead of copy pasting the Google Analytics code on every .html file, is there
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?
More posts by @Karen161
8 Comments
Sorted by latest first Latest Oldest Best
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.
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.
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.
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>
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.
FYI
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 -->
</head>
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-...']);
_gaq.push(['_trackPageview']);
...
</script>
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"); ?>
</head>
Server Side Includes could be your way forward.
en.wikipedia.org/wiki/Server_Side_Includes
They are generally ignored because scripting languages like PHP do a better job, but these will get the job done for you.
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.
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.