258
Views
20
Comments
How to add Google Analytics Tracking Code
Question
Hello All,

Whats the easiest way to add the following code to every page in my site in the head section of the html?

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-177854-2']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'https://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

Im not sure where to start. I tried pasting this code directly to the espace javascript section but it didnt work out. Please help if you can.

Julius

2019-11-12 17-31-26
Justin James
 
MVP

Julius -

Putting it into the eSpace's JavaScript is correct, just make sure you omit the beginning and ending script tags.

J.Ja

UserImage.jpg
Julius Dickens
Justin,

Thanks for the advice. I did omit the script tags and it still isnt showing up in the html. Is there something I need to add so that the system knows to add it to the head section?

Julius
2019-11-12 17-31-26
Justin James
 
MVP
Julius -

You aren't going to see it in the HTML, it gets linked to as an external .js file. :)

J.Ja
2024-11-04 15-59-50
João Inácio
Justin James wrote:
Julius -

You aren't going to see it in the HTML, it gets linked to as an external .js file. :)

J.Ja
 
 I also could not find it in any .js file. Do you know why?

Joop Stringer-> Using an expression, is not a good solution (for this case) since i need that code to be inside header

Best Regards

João inácio
2011-06-15 10-51-22
Joop Stringer
João Inácio wrote:
 
 I also could not find it in any .js file. Do you know why?

Joop Stringer-> Using an expression, is not a good solution (for this case) since i need that code to be inside header

Best Regards

João inácio
 
 Joao:
This script still works :-)
The one in the header is the latest 
2024-11-04 15-59-50
João Inácio
João Inácio wrote:
 
 I also could not find it in any .js file. Do you know why?

Joop Stringer-> Using an expression, is not a good solution (for this case) since i need that code to be inside header

Best Regards

João inácio
 

Easiest solution.

 
2011-06-15 10-51-22
Joop Stringer
The other solution is to put it in an expression in the header of footer webblock which resides on every page.
Be sure to enclose the script in double quotes and replace existing double quotes with double-double quotes :-)

"<script type=""text/javascript"">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-177854-2']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'https://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>"


 

UserImage.jpg
Charles Hewitt
Hi Joop

Do you have any suggestions how to include the logged in Master user name in the Google Analytics ie as a Custom Variable key or User Defined variable.  At the moment, the User Defined variable is showing as (not set) in google analytics.

Thanks
Charles

Joop Stringer wrote:
The other solution is to put it in an expression in the header of footer webblock which resides on every page.
Be sure to enclose the script in double quotes and replace existing double quotes with double-double quotes :-)

"<script type=""text/javascript"">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-177854-2']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'https://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>"


 

 
 
2011-06-15 10-51-22
Joop Stringer
Charles Hewitt wrote:
 Do you have any suggestions how to include the logged in Master user name in the Google Analytics
 
 
 To get any other information in Google Analytics, just add it to the _gaq.push function:
        _gaq.push(['_trackEvent', 'Menu', 'Main item', this.href]);
Or
        _gaq.push([ '_trackUser', 'User', UserName ] );


Check this out : https://developers.google.com/analytics/devguides/collection/gajs/methods/gaJSApi_gaq#_gaq.push
Complete explanation on how to use _gaq.push !!
2025-11-03 12-56-18
Evert van der Zalm
 
MVP
Joop Stringer wrote:
The other solution is to put it in an expression in the header of footer webblock which resides on every page.
 
 Joop,

Small (performance) addition information for this:

Put the JS in a separe webblock an include that webblock into the footer. Besides it's easier to maintain (one webblock for the GA JS code), you can also set a own caching time on it (webblock property). Oke there is a default caching in the IIS (OS defult is 30 days), but if that one isn't an option you can manipilate it by this.

Kind reagards,
Evert
UserImage.jpg
Charles Hewitt
Yes, I have the basic analytics working, but it does not record the Master User Name - or any other user name in the analytics data collected...
I would like to identify by high value users etc and track problems individual users have.
2022-11-12 11-28-30
Gonçalo Martins
Staff
Hi all,

Did anyone integrated Google Analytics in a page, by using the action "AddPostProcessingFilter" of the extension "HTTPRequestHandler", replacing the tag "</head>" with the "<snipet> + </head>" ?
I have to add the script to the head, since we have a dynamic model to do this.
Hope someone helps.

Kind Regards,
GM


2022-11-12 11-28-30
Gonçalo Martins
Staff
Hi all again,

Did anyone used the action "AddPostProcessingFilter"?
I did some different tests beside the Google Analystics and I got no results.
If anyone could share some experience about this, I would appreciate a lot.


Kind Regards,
GM


2025-11-03 12-56-18
Evert van der Zalm
 
MVP
Hello Goncalo,

I've used it in some code, have attached a screenshot of the useage, hopfully it's enough for you to understand it, else let me know.

Kind regards,
Evert
AddPostProcessing.png
2022-11-12 11-28-30
Gonçalo Martins
Staff
Hello Evert,

Thanks a lot..My mistake was very silly to be honest..It was in the regex, since I forgot to put the chars in the correct form.
You saved my day  :)

Kind Regards,
GM

Evert van der Zalm wrote:
Hello Goncalo,

I've used it in some code, have attached a screenshot of the useage, hopfully it's enough for you to understand it, else let me know.

Kind regards,
Evert
 
 
2025-11-03 12-56-18
Evert van der Zalm
 
MVP

Hello Goncalo,

That isn't silly, I've had the same problem (-;

Kind regards,
Evert
2016-03-09 17-21-39
Fabian
Hi Evert
I can get this to work when the regex is looking for the title tag. but i need to add my code to the end of the head tag. When i use <\/head> in the regex nothing is added. It's as if the head tag doesn't exist when i call the AddPostProcessingFilter in the Header Preparation. Any help would be appreciated
Thank you.
2023-12-07 07-51-40
Remco Dekkinga
 
MVP
Hi Fabian, Google isn't that strict. You can add the code before/after the title tag and it should work. 
2016-03-09 17-21-39
Fabian
Thank you Remco.

I was also told that technically GA works anywhere on a page. However, data accuracy is greatest when placed as the last element of the head section. Supposedly this is best practice.

So putting it right after the title tag should work but putting it at the end of the head tag would be best.
I'm just wondering why the same code that works for adding GA after the title doesn't work for when i try adding it before the end of head.
2025-11-03 12-56-18
Evert van der Zalm
 
MVP

Hello Fabian,

Also didn't got that to work. I assumed the filledin regex wasn't the correct one, but didn't want to google it to find it out.

The AddPostProcessingFilter action was a workaround since we couldn't get a JS in the header of a page. This is now also possible by add the JS in the javascript field of an webblock or webscreen (like the post of 'João Inácio').

Kind regards,
Evert
Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.