Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit f8e879cac30562652c4621e40a0ca8289dba11ec @andrew andrew committed Jun 21, 2011
Showing with 188 additions and 0 deletions.
  1. +17 −0 .gitignore
  2. +3 −0 Gemfile
  3. +7 −0 Rakefile
  4. +56 −0 Readme.mdown
  5. +38 −0 lib/split/analytics.rb
  6. +5 −0 lib/split/analytics/version.rb
  7. +19 −0 spec/analytics_spec.rb
  8. +21 −0 spec/spec_helper.rb
  9. +22 −0 split-analytics.gemspec
@@ -0,0 +1,17 @@
+*.gem
+*.rbc
+.bundle
+.config
+.yardoc
+Gemfile.lock
+InstalledFiles
+_yardoc
+coverage
+doc/
+lib/bundler/man
+pkg
+rdoc
+spec/reports
+test/tmp
+test/version_tmp
+tmp
@@ -0,0 +1,3 @@
+source 'http://rubygems.org'
+
+gemspec
@@ -0,0 +1,7 @@
+#!/usr/bin/env rake
+require 'bundler/gem_tasks'
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new('spec')
+
+task :default => :spec
@@ -0,0 +1,56 @@
+# Split::Analytics
+
+An extension to [Split](http://github.com/andrew/split) to push test data to google analytics.
+
+## Requirements
+
+The split gem and its dependencies.
+
+## Setup
+
+If you are using bundler add split to your Gemfile:
+
+ gem 'split-analytics', :require => 'split/analytics'
+
+Then run:
+
+ bundle install
+
+Otherwise install the gem:
+
+ gem install split-analytics
+
+and require it in your project:
+
+ require 'split/analytics'
+
+## Usage
+
+Use in your application layout template
+
+ # erb
+ <%= tracking_code(:account => 'UA-12345-6') %>
+
+ # haml
+ = tracking_code(:account => 'UA-12345-6')
+
+## Development
+
+Source hosted at [GitHub](http://github.com/andrew/split-analytics).
+Report Issues/Feature requests on [GitHub Issues](http://github.com/andrew/split-analytics/issues).
+
+Tests can be ran with `rake spec`
+
+### Note on Patches/Pull Requests
+
+ * Fork the project.
+ * Make your feature addition or bug fix.
+ * Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+ * Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
+ * Send me a pull request. Bonus points for topic branches.
+
+## Copyright
+
+Copyright (c) 2011 Andrew Nesbitt. See LICENSE for details.
@@ -0,0 +1,38 @@
+require "split/helper"
+
+module Split
+ module Analytics
+ def tracking_code(options={})
+ # needs more options: http://code.google.com/apis/analytics/docs/gaJS/gaJSApi.html
+ account = options.delete(:account)
+
+ <<-EOF
+ <script type="text/javascript">
+ var _gaq = _gaq || [];
+ _gaq.push(['_setAccount', '#{account}']);
+ #{custom_variables}
+ _gaq.push(['_trackPageview']);
+
+ (function() {
+ var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
+ ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
+ var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
+ })();
+ </script>
+ EOF
+ end
+
+ def custom_variables
+ return nil if session[:split].nil?
+ session[:split].map do |k,v|
+ "_gaq.push(['_setCustomVar', 1, '#{k}', '#{v}', 1]);"
+ end.join("\n")
+ end
+ end
+end
+
+module Split::Helper
+ include Split::Analytics
+end
+
+# mix Spilt::Analytics into Split::Helpers
@@ -0,0 +1,5 @@
+module Split
+ module Analytics
+ VERSION = "0.2.2"
+ end
+end
@@ -0,0 +1,19 @@
+require 'spec_helper'
+
+describe Split::Analytics do
+ include Split::Helper
+ it "should generate valid analytics javascript" do
+ tracking_code = tracking_code(:account => 'UA-12345-6')
+ tracking_code.should eql(" <script type=\"text/javascript\">\n var _gaq = _gaq || [];\n _gaq.push(['_setAccount', 'UA-12345-6']);\n \n _gaq.push(['_trackPageview']);\n\n (function() {\n var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n })();\n </script>\n")
+ end
+
+ it "should add custom variables for every test the user is involved in" do
+ first_alt = ab_test('link_colour', 'red', 'blue')
+ second_alt = ab_test('link_text', 'Join', 'Signup')
+
+ session[:split].should eql({'link_colour' => first_alt,'link_text' => second_alt})
+
+ tracking_code = tracking_code(:account => 'UA-12345-6')
+ tracking_code.should eql(" <script type=\"text/javascript\">\n var _gaq = _gaq || [];\n _gaq.push(['_setAccount', 'UA-12345-6']);\n _gaq.push(['_setCustomVar', 1, 'link_colour', '#{first_alt}', 1]);\n_gaq.push(['_setCustomVar', 1, 'link_text', '#{second_alt}', 1]);\n _gaq.push(['_trackPageview']);\n\n (function() {\n var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;\n ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';\n var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);\n })();\n </script>\n")
+ end
+end
@@ -0,0 +1,21 @@
+require 'rubygems'
+require 'bundler/setup'
+require 'split'
+require 'split/helper'
+require 'split/analytics'
+require 'ostruct'
+
+def session
+ @session ||= {}
+end
+
+def params
+ @params ||= {}
+end
+
+def request(ua = 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; de-de) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27')
+ r = OpenStruct.new
+ r.user_agent = ua
+ r.ip = '192.168.1.1'
+ @request ||= r
+end
@@ -0,0 +1,22 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "split/analytics/version"
+
+Gem::Specification.new do |gem|
+ gem.authors = ["Andrew Nesbitt"]
+ gem.email = ["andrewnez@gmail.com"]
+ gem.summary = %q{Split extension to push test data to google analytics}
+ gem.homepage = "https://github.com/andrew/split-analytics"
+
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ gem.files = `git ls-files`.split("\n")
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ gem.name = "split-analytics"
+ gem.require_paths = ['lib']
+ gem.version = Split::Analytics::VERSION
+
+ gem.add_dependency(%q<split>, ["~> 0.2.2"])
+
+ # Development Dependencies
+ gem.add_development_dependency(%q<rspec>, ["~> 2.6"])
+end

0 comments on commit f8e879c

Please sign in to comment.