Coder Social home page Coder Social logo

google_analytics's Introduction

GoogleAnalytics

Google Analytics Javascript plugin implements the following methods which
generate the necessary GA javascript (minus the script tags): -

  • ga_generate_js(‘YOUR-UA’)
    Produces new async style GA javascript: -
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'YOUR-UA']);
_gaq.push(['_trackPageview']);

(function() {
  var ga = document.createElement('script');
  ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 
      'http://www') + '.google-analytics.com/ga.js';
  ga.setAttribute('async', 'true');
  document.documentElement.firstChild.appendChild(ga);
})();
  • ga_generate_non_async_js(‘YOUR-UA’)
    Produces the non async style GA 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"));
try {
	var pageTracker = _gat._getTracker("YOUR-UA");
	pageTracker._trackPageview();
} catch(err) {}

Example

Inside you app/views/layouts/application.html.erb (using jQuery): -

<% javascript_tag :defer => 'defer' do %>
  $(document).ready(function() {
    <%= yield :script %>
    <%= ga_generate_js('UA-1234-5') %>
  });
<% end %>

Links

Async GA tracking – http://googlecode.blogspot.com/2009/12/google-analytics-launches-asynchronous.html

Copyright © 2009 [Ash McKenzie], released under the MIT license

google_analytics's People

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.