Coder Social home page Coder Social logo

ga_events's Introduction

GaEvents

Use Google Analytics' Event Tracking everywhere in your Rails app!

This gem alllows you to annotate events everywhere in the code of your Rails app. A rack middleware is automatically inserted into the stack. It transports the event data to the client. Normal requests get a DIV injected, Ajax requests get a data-pounded custom HTTP header appended. In case of redirects the data survives inside Rails' flash. The asset pipeline-ready CoffeeScript extracts this data on the client-side and pushes it to Google Analytics via ga.js or Google Tag Manager.

Dependencies

  • Rails 3.1 onwards
  • jQuery

Installation

Add it to your Gemfile with:

gem 'ga_events'

Run the bundle command to install it.

Add to the top of your application.js (but after requiring jQuery):

//= require ga_events.js

After requiring ga_events.js, choose an adapter.

For stock Google Analytics (ga.js) use:

GaEvents.Event.adapter = function() {
  return new GaEvents.GoogleAnalyticsAdapter();
}

If you are using Google Tag Manager you can add custom events which are then passed through to Google Analytics.

GaEvents.Event.adapter = function() {
  return new GaEvents.GoogleTagManagerAdapter("event_name"); // defaults to ga_event
}

If you are using a staging system you can use the NullAdapter.

GaEvents.Event.adapter = function() {
  return new GaEvents.NullAdapter();
}

Usage

On the server-side a new event is added to a list, serialized into a container element and then added to your HTML response. On Ajax requests a custom HTTP header is added to the response.

You can create a new event like this:

GaEvents::Event.new(category, action, label, value)

On the client-side there is a similar interface to GaEvents:

new GaEvents.Event(category, action, label, value)

We have taken special care of tracking events while the DOM is loading. Events get collected until the DOM is ready and flushed afterwards.

Too many events

Use something like this snippet to get informed of bloating HTTP headers with event data:

class ApplicationController < ActionController::Base
  after_filter :too_many_ga_events?
  private
  def too_many_ga_events?
    if (serialized = GaEvents::List.to_s).length > 1_024
      notify("GaEvents too big: #{serialized}")
    end
    true
  end
end

Contributing

Yes please! Use pull requests.

More docs and tools

ga_events's People

Contributors

danielbayerlein avatar der-flo avatar jhilden avatar svenwin avatar

Watchers

 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.