Skip to content
This repository has been archived by the owner on Aug 10, 2020. It is now read-only.

Include name_tag method if name is passed as a parameter to identify #31

Merged
merged 2 commits into from Dec 9, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/analytical/modules/mixpanel.rb
Expand Up @@ -28,7 +28,7 @@ def init_javascript(location)

def track(event, properties = {})
callback = properties.delete(:callback) || "function(){}"
"mpmetrics.track('#{event}', #{properties.to_json}, #{callback});"
%(mpmetrics.track("#{event}", #{properties.to_json}, #{callback});)
end

# Used to set "Super Properties" - http://mixpanel.com/api/docs/guides/super-properties
Expand All @@ -37,11 +37,14 @@ def set(properties)
end

def identify(id, *args)
"mpmetrics.identify('#{id}');"
opts = args.first || {}
name = opts.is_a?(Hash) ? opts[:name] : ""
name_str = name.blank? ? "" : " mpmetrics.name_tag('#{name}');"
%(mpmetrics.identify('#{id}');#{name_str})
end

def event(name, attributes = {})
"mpmetrics.track('#{name}', #{attributes.to_json});"
%(mpmetrics.track("#{name}", #{attributes.to_json});)
end

end
Expand Down
10 changes: 7 additions & 3 deletions spec/analytical/modules/mixpanel_spec.rb
Expand Up @@ -19,21 +19,25 @@
@api = Analytical::Modules::Mixpanel.new :parent=>@parent, :js_url_key=>'abcdef'
@api.identify('id', {:email=>'test@test.com'}).should == "mpmetrics.identify('id');"
end
it 'should return a js string with name if included' do
@api = Analytical::Modules::Mixpanel.new :parent=>@parent, :js_url_key=>'abcdef'
@api.identify('id', {:email=>'test@test.com', :name => "user_name"}).should == "mpmetrics.identify('id'); mpmetrics.name_tag('user_name');"
end
end
describe '#track' do
it 'should return the tracking javascript' do
@api = Analytical::Modules::Mixpanel.new :parent=>@parent, :key=>'abcdef'
@api.track('pagename', {:some=>'data'}).should == "mpmetrics.track('pagename', {\"some\":\"data\"}, function(){});"
@api.track('pagename', {:some=>'data'}).should == "mpmetrics.track(\"pagename\", {\"some\":\"data\"}, function(){});"
end
it 'should return the tracking javascript with a callback' do
@api = Analytical::Modules::Mixpanel.new :parent=>@parent, :key=>'abcdef'
@api.track('pagename', {:some=>'data', :callback=>'fubar'}).should == "mpmetrics.track('pagename', {\"some\":\"data\"}, fubar);"
@api.track('pagename', {:some=>'data', :callback=>'fubar'}).should == "mpmetrics.track(\"pagename\", {\"some\":\"data\"}, fubar);"
end
end
describe '#event' do
it 'should return a js string' do
@api = Analytical::Modules::Mixpanel.new :parent=>@parent, :js_url_key=>'abcdef'
@api.event('An event happened', { :item => 43 }).should == "mpmetrics.track('An event happened', {\"item\":43});"
@api.event('An event happened', { :item => 43 }).should == "mpmetrics.track(\"An event happened\", {\"item\":43});"
end
end
describe '#init_javascript' do
Expand Down