Skip to content

Commit

Permalink
basic testing and don't add distinct id if it doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
pwim committed Apr 2, 2012
1 parent 42ae0f3 commit 270380c
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -3,3 +3,4 @@
Gemfile.lock
pkg/*
.rvmrc
spec/app/log/*
4 changes: 3 additions & 1 deletion lib/mixpanel_rails.rb
Expand Up @@ -32,7 +32,9 @@ def process_mixpanel_queue
unless response.redirect_url && request.host == URI.parse(response.redirect_url).host
mixpanel = Mixpanel::Tracker.new(MixpanelRails::Railtie.config.mixpanel_rails.token, request.env, true)
distinct_id = mixpanel_distinct_id.bind(self).call
params = {:distinct_id => distinct_id}.merge(register_with_mixpanel)
params = {}
params[:distinct_id] = distinct_id if distinct_id
params.merge!(register_with_mixpanel)
if request.env["Rack-Middleware-PDFKit"] || response.redirect_url
mixpanel_queue.each {|s| mixpanel.track_event(s, params) }
else
Expand Down
7 changes: 7 additions & 0 deletions mixpanel_rails.gemspec
Expand Up @@ -19,4 +19,11 @@ Gem::Specification.new do |s|
s.require_paths = ["lib"]

s.add_dependency 'mixpanel', '~> 1.0.0'
s.add_dependency 'rails', '~> 3.0'

s.add_development_dependency 'rspec', '~> 2.5'
s.add_development_dependency 'capybara', '~> 1.1.1'
s.add_development_dependency 'steak'
s.add_development_dependency 'steak'
s.add_development_dependency 'rspec-rails'
end
9 changes: 9 additions & 0 deletions spec/app/app/views/layouts/application.html.erb
@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%= yield %>
</body>
</html>
18 changes: 18 additions & 0 deletions spec/app/fake.rb
@@ -0,0 +1,18 @@
require 'bundler/setup'
require 'rails'
Bundler.require(:default, Rails.env)
require 'action_controller/railtie'
require 'action_view/railtie'

app = Class.new(Rails::Application)
app.config.root = File.dirname(__FILE__)
app.config.secret_token = "3b7cd727ee24e8444053437c36cc66c4"
app.config.active_support.deprecation = :log
app.config.mixpanel_rails.token = "test_token"
app.initialize!

app.routes.draw do
match ':controller(/:action(/:id))'
end

class ApplicationController < ActionController::Base; end
79 changes: 79 additions & 0 deletions spec/mixpanel_rails_spec.rb
@@ -0,0 +1,79 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')

class ExampleController < ApplicationController
uses_mixpanel

def no_tracking
render inline: "OK", layout: true
end

def with_tracking
track_with_mixpanel "Register for site"
render inline: "OK", layout: true
end
end

class RedirectsController < ApplicationController
uses_mixpanel

def index
track_with_mixpanel "Register for site"
redirect_to action: :redirected
end

def redirected
render inline: "OK", layout: true
end
end

class DistinctIdController < ApplicationController
uses_mixpanel distinct_id: lambda { 1 }

def index
render inline: "OK", layout: true
end
end

shared_examples_for "mixpanel init" do
it do
page.find("script:first").text.should include('mpq.push(["init", "test_token"]);')
end
end

shared_examples_for "without distinct id" do
it { page.find("script:last").text.should_not include('distinct_id') }
end

shared_examples_for "with distinct id" do
it { page.find("script:last").text.should include('mpq.push(["register", {"distinct_id":1}]);') }
end

shared_examples_for "with event" do
it { page.find("script:last").text.should include('mpq.push(["track", "Register for site"])') }
end

feature 'mixpanel integration' do
context 'visit page without tracking' do
background { visit '/example/no_tracking' }
it_should_behave_like "mixpanel init"
it_should_behave_like "without distinct id"
end

context 'visit page with tracking' do
background { visit '/example/with_tracking' }
it_should_behave_like "mixpanel init"
it_should_behave_like "without distinct id"
it_should_behave_like "with event"
end

context 'visit page with distinct id' do
background { visit '/distinct_id' }
it_should_behave_like "mixpanel init"
it_should_behave_like "with distinct id"
end

context 'follow redirect' do
background { visit '/redirects' }
it_should_behave_like "with event"
end
end
5 changes: 5 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,5 @@
$:.unshift File.expand_path('../../lib', __FILE__)
require File.expand_path(File.dirname(__FILE__) + "/app/fake")
require 'rspec/rails'
require "steak"
require 'capybara/rspec'

0 comments on commit 270380c

Please sign in to comment.