简体   繁体   中英

Looking to update from google-analytics.com/ga.js to stats.g.doubleclick.net/dc.js with old code

I would like to change google-analytics.com/ga.js to stats.g.doubleclick.net/dc.js

My current code (old, but afraid to change it -- inherited) is

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :   "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js'     type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
try {
var pageTracker = _gat._getTracker('UA-051510-6'); 
pageTracker._setDomainName('mydomain.org'); 
pageTracker._addIgnoredOrganic('mydomain.org'); 
pageTracker._setAllowHash(false);
pageTracker._setAllowLinker(true);
pageTracker._trackPageview();
} catch(err) {}</script>

I tried changing it to

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :   "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "stats.g.doubleclick.net/dc.js'     type='text/javascript'%3E%3C/script%3E"));
</script>

But got an error saying it couldn't load, since it was looking in localhost:dc.js

Any help appreciated!

I think you should update to Classic async code (Universal Analytics doesn't support doubleclick yet) like this:

<script type="text/javascript">

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-051510-6']);
  _gaq.push(['_setDomainName', 'mydomain.org']);
  _gaq.push(['_setAllowLinker', true]);
  _gaq.push(['_addIgnoredOrganic', 'mydomain.org']); 
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'stats.g.doubleclick.net/dc.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>

_setAllowHash is deprecated

Thanks for all who helped. Essentially I just had to remove the www since it was tyring to pull www.stats.g.doubleclick.net/dc.js

So final code was

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." :   "http://");
document.write(unescape("%3Cscript src='" + gaJsHost + "stats.g.doubleclick.net/dc.js'          type='text/javascript'%3E%3C/script%3E"));
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM