garails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in garails.gemspec
4
+ gemspec
5
+
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ garails (0.0.1)
5
+ gabbara (~> 0.0.2)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ activesupport (3.0.3)
11
+ gabbara (0.0.2)
12
+ activesupport
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ activesupport
19
+ gabbara (~> 0.0.2)
20
+ garails!
data/README.rdoc ADDED
@@ -0,0 +1,45 @@
1
+ == Google Analytics for Rails
2
+
3
+ Complete package to use google analytics in your rails application. It supports recording to google analytics:
4
+
5
+ * for normal web browsers (javascript)
6
+ * for mobile handsets (gif)
7
+ * programmatically (events)
8
+
9
+ == Installation
10
+
11
+ Add to your Gemfile:
12
+
13
+ gem 'garails'
14
+
15
+ and run 'bundle install'.
16
+
17
+ Then create a config/initializers/garails.rb with the following content:
18
+
19
+ Garails.ga_account = 'UA-12345678-9'
20
+ # Garails.ga_domain = '.mydomain.com'
21
+
22
+ == Usage
23
+
24
+ === for normal web browsers
25
+
26
+ Add to your layout (usually app/views/layouts/application.html.erb) in the head section:
27
+
28
+ <html>
29
+ <head>
30
+ <title>My App</title>
31
+ ...
32
+ <%= google_analytics_tracking_javascript %>
33
+ </head>
34
+ <body>
35
+ ...
36
+
37
+
38
+ === for mobile handsets (non-Javascript)
39
+
40
+ In your layout for mobile handsets without Javascript support, add at the very bottom of the body the following:
41
+
42
+ <%= utm_tag %>
43
+
44
+ === programmatically
45
+
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake/testtask'
2
+ require 'rake/rdoctask'
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ task :default => :test
7
+
8
+ desc 'Run unit tests.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'Google Analytics for Rails'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README.rdoc')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
@@ -0,0 +1,43 @@
1
+ require 'gabbara'
2
+ require 'digest'
3
+
4
+ class Garails::GoogleAnalyticsController < ApplicationController
5
+ unloadable
6
+ before_filter :extract_visitor_id
7
+
8
+ UTM_HEADERS = {
9
+ "Content-Type" => "image/gif",
10
+ "Cache-Control" => "private, no-cache, no-cache=Set-Cookie, proxy-revalidate",
11
+ "Pragma" => "no-cache",
12
+ "Expires" => "Wed, 17 Sep 1975 21:32:10 GMT" }
13
+ UTM_GIF_DATA = [
14
+ 0x47, 0x49, 0x46, 0x38, 0x39, 0x61, 0x01, 0x00, 0x01, 0x00, 0x80, 0xff,
15
+ 0x00, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0x00,
16
+ 0x01, 0x00, 0x01, 0x00, 0x00, 0x02, 0x02, 0x44, 0x01, 0x00, 0x3b ].map{|i| i.chr }.join
17
+ UTM_COOKIE_NAME = "__utmmobile"
18
+
19
+ def utm
20
+ if Garails.ga_setup?
21
+ g = Garails.mobile_gabba(request, :utmn => params[:utmn])
22
+ g.page_view('', :utmvid => @visitor_id)
23
+ end
24
+ response.headers.merge(UTM_HEADERS)
25
+ respond_to do |format|
26
+ format.gif { send_data UTM_GIF_DATA, :type => :gif, :disposition => "inline" }
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def extract_visitor_id
33
+ @visitor_id = cookies[UTM_COOKIE_NAME] || construct_new_visitor_id
34
+ cookies[UTM_COOKIE_NAME] = {:value => @visitor_id, :expires => Time.now + 2.years, :path => '/'}
35
+ end
36
+
37
+ def construct_new_visitor_id
38
+ raw_visitor_id = request.env["HTTP_X_DCMGUID"].blank? ?
39
+ ((request.env["HTTP_USER_AGENT"] || "") + rand(0x7fffffff).to_s) :
40
+ (request.env["HTTP_X_DCMGUID"] + (Garails.ga_account || 'unknown'))
41
+ "0x" + Digest::MD5.hexdigest(raw_visitor_id)[0, 16]
42
+ end
43
+ end
@@ -0,0 +1,6 @@
1
+ module Garails::JavascriptHelper
2
+ def google_analytics_tracking_javascript
3
+ return "" if Garails.ga_account.blank?
4
+ render :template => "garails/google_analytics"
5
+ end
6
+ end
@@ -0,0 +1,19 @@
1
+ module Garails::MobileHelper
2
+
3
+ def utm_tag
4
+ image_tag utm_url, :alt => "", :size => "1x1"
5
+ end
6
+
7
+ def utm_url
8
+ referer = request.env['HTTP_REFERER']
9
+ params = {
10
+ :utmn => rand(0x7fffffff).to_s,
11
+ :utmr => referer.blank? ? "-" : referer,
12
+ :utmp => request.env['REQUEST_URI'],
13
+ :guid => "ON",
14
+ :format => :gif
15
+ }
16
+ super(params)
17
+ end
18
+
19
+ end
@@ -0,0 +1,15 @@
1
+ <script type='text/javascript'>
2
+ //<![CDATA[
3
+ var _gaq = _gaq || [];
4
+ _gaq.push(['_setAccount', '<%= Garails.ga_account %>']);
5
+ <% unless Garails.ga_domain.blank? -%>
6
+ _gaq.push(['_setDomainName', '<%= Garails.ga_domain %>']);
7
+ <% end -%>
8
+ _gaq.push(['_trackPageview']);
9
+ (function() {
10
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
11
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
12
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
13
+ })();
14
+ //]]>
15
+ </script>
@@ -0,0 +1 @@
1
+ Mime::Type.register "image/gif", :gif
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ match "utm" => "garails/google_analytics#utm", :as => "utm", :format => :gif
3
+ end
data/garails.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "garails/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "garails"
7
+ s.version = Garails::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = 'Michael Reinsch'
10
+ s.email = "michael@mobalean.com"
11
+ s.homepage = ""
12
+ s.description = "Google Analytics for Rails"
13
+ s.summary = "Google Analytics for Rails"
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency("gabbara", "~> 0.0.2")
21
+ end
@@ -0,0 +1,9 @@
1
+ require "rails"
2
+
3
+ module Garails
4
+ class Engine < ::Rails::Engine
5
+ initializer 'garails.mobile.helper' do |app|
6
+ ActionController::Base.helper Garails::MobileHelper, Garails::JavascriptHelper
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Garails
2
+ VERSION = '0.0.1'
3
+ end
data/lib/garails.rb ADDED
@@ -0,0 +1,38 @@
1
+ module Garails
2
+
3
+ # account identifier as issued by google analytics
4
+ mattr_accessor :ga_account
5
+ @@ga_account = nil
6
+
7
+ # domain setting. Only set this if you need to (e.g. for subdomain tracking).
8
+ # Corresponds to "_setDomainName".
9
+ mattr_accessor :ga_domain
10
+ @@ga_domain = nil
11
+
12
+ def self.ga_setup?
13
+ ! Garails.ga_account.blank?
14
+ end
15
+
16
+ def self.ga_mobile_account
17
+ ga_account.sub(/^UA-/, 'MO-')
18
+ end
19
+
20
+ def self.gabba(request, opts = {})
21
+ create_gabba(Garails.ga_account, request, opts)
22
+ end
23
+
24
+ def self.mobile_gabba(request, opts = {})
25
+ create_gabba(Garails.ga_mobile_account, request, opts)
26
+ end
27
+
28
+ def self.create_gabba(account, request, opts = {})
29
+ Gabbara::Gabba.new(account, Garails.ga_domain, opts).tap do |g|
30
+ g.logger = Rails.logger
31
+ g.request = request
32
+ end
33
+ end
34
+ end
35
+
36
+ require 'garails/version'
37
+ require 'garails/engine'
38
+
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: garails
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Michael Reinsch
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-20 00:00:00 +09:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: gabbara
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 27
30
+ segments:
31
+ - 0
32
+ - 0
33
+ - 2
34
+ version: 0.0.2
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: Google Analytics for Rails
38
+ email: michael@mobalean.com
39
+ executables: []
40
+
41
+ extensions: []
42
+
43
+ extra_rdoc_files: []
44
+
45
+ files:
46
+ - Gemfile
47
+ - Gemfile.lock
48
+ - README.rdoc
49
+ - Rakefile
50
+ - app/controllers/garails/google_analytics_controller.rb
51
+ - app/helpers/garails/javascript_helper.rb
52
+ - app/helpers/garails/mobile_helper.rb
53
+ - app/views/garails/google_analytics.html.erb
54
+ - config/initializers/mime_types.rb
55
+ - config/routes.rb
56
+ - garails.gemspec
57
+ - lib/garails.rb
58
+ - lib/garails/engine.rb
59
+ - lib/garails/version.rb
60
+ has_rdoc: true
61
+ homepage: ""
62
+ licenses: []
63
+
64
+ post_install_message:
65
+ rdoc_options: []
66
+
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ requirements: []
88
+
89
+ rubyforge_project:
90
+ rubygems_version: 1.3.7
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Google Analytics for Rails
94
+ test_files: []
95
+