function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rocio VivancoRocio Vivanco 

How to embed a Google Analytics tracking code in a Visualforce page?

Hi all,

I don't know if this is the suitable place to post this thread.
I have to embed a Google Analytics code to my Visualforce page. The VF page is already published on the Web through a created site.

I added the GA code between the <script> </script> in the head tag in the visualforce this way:
 
<script>
 (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-mycode-1', 'auto', {'allowLinker': true});
  ga('require', 'linker');
  ga('linker:autoLink', ['mycompany.com'] );

</script>
The Visualforce URL is something like this:
 
https://mycompany.secure.force.com/myfolder/myvisualforcepage
Is the GA code embedded in my Visualforce correct?, especially:

1. Is the domain name in the last line correct?
2. Is there something missing there?. The aim is to get the VF page tracked.

I would appreciate any help.

 
Srinivas d 7Srinivas d 7
https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_compref_site_googleAnalyticsTracking.htm