Skip to content

Instantly share code, notes, and snippets.

@scottwwhitman
Last active May 16, 2022 00:34
Show Gist options
  • Save scottwwhitman/06899fd92abcc709da56baccc2b4dd94 to your computer and use it in GitHub Desktop.
Save scottwwhitman/06899fd92abcc709da56baccc2b4dd94 to your computer and use it in GitHub Desktop.
Add Google Analytics to your Rails app

Google Analytics for Rails

Fast Universal Google Analytics setup for Rails. This gem is mostly intended for small to medium websites with a simple analytics strategy.

Why use Google Analytics?

  • Google Analytics generates detailed statistics about a website's traffic and traffic sources
  • Also measures conversions and sales
  • Most widely used website statistics service
  • Basic service is free of charge and a premium version is available for a fee

    Why is it important?

  • Provides loads of information on your users and their behavior
  • Tracks data that impacts your sites' rankings in google search results

    What does it give you?

    alt text

    Setup

    First, sign up for a google analytics account:

    Add the following to your Gemfile:

    gem 'google-analytics-rails', '1.1.0'
    

    Then run:

    bundle install
    

    Basic configuration:

    Production only

    config/environments/production.rb:

    # replace this with your tracker code
    GA.tracker = "UA-112233-4"
    

    app/views/layout/application.html.erb, in the <head> tag :

    	<%= analytics_init if GoogleAnalytics.valid_tracker? %>
    

    Different configurations also available for:

  • With DoubleClick instead of vanilla Google Analytics script
  • Different accounts for staging and production
  • Premium Google Analytics accounts

    ISSUE TO NOTE - Turbolinks compatibility:

    Many developers have reported problems with page view tracking both with and without Turbolinks

    Resources with detail on potential fixes:

    Documentation & Resources:

  • Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment