From 49b4645790d9dcba02f32cf311cad070d75b5f72 Mon Sep 17 00:00:00 2001 From: Brandon Leonardo Date: Thu, 8 Dec 2011 23:36:14 -0800 Subject: [PATCH 1/2] include name_tag method if name is provided as a parameter for mixpanel identity --- lib/analytical/modules/mixpanel.rb | 5 ++++- spec/analytical/modules/mixpanel_spec.rb | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/analytical/modules/mixpanel.rb b/lib/analytical/modules/mixpanel.rb index 879e164..58c36cc 100644 --- a/lib/analytical/modules/mixpanel.rb +++ b/lib/analytical/modules/mixpanel.rb @@ -37,7 +37,10 @@ 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 = {}) diff --git a/spec/analytical/modules/mixpanel_spec.rb b/spec/analytical/modules/mixpanel_spec.rb index df1be63..75319ae 100644 --- a/spec/analytical/modules/mixpanel_spec.rb +++ b/spec/analytical/modules/mixpanel_spec.rb @@ -19,6 +19,10 @@ @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 From 6233a900a1f2bc7ddf23fbe02f817042fa7cfcf4 Mon Sep 17 00:00:00 2001 From: Brandon Leonardo Date: Fri, 9 Dec 2011 00:26:26 -0800 Subject: [PATCH 2/2] use full quotes around event and page names when generating track calls because the _analytical_javascript regular expression that converts those __EVENT__ and __PAGE__ to parameter names is dependent on double quotes (cant be single quotes) --- lib/analytical/modules/mixpanel.rb | 6 +++--- spec/analytical/modules/mixpanel_spec.rb | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/analytical/modules/mixpanel.rb b/lib/analytical/modules/mixpanel.rb index 58c36cc..18c4b4a 100644 --- a/lib/analytical/modules/mixpanel.rb +++ b/lib/analytical/modules/mixpanel.rb @@ -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 @@ -40,11 +40,11 @@ def identify(id, *args) opts = args.first || {} name = opts.is_a?(Hash) ? opts[:name] : "" name_str = name.blank? ? "" : " mpmetrics.name_tag('#{name}');" - "mpmetrics.identify('#{id}');#{name_str}" + %(mpmetrics.identify('#{id}');#{name_str}) end def event(name, attributes = {}) - "mpmetrics.track('#{name}', #{attributes.to_json});" + %(mpmetrics.track("#{name}", #{attributes.to_json});) end end diff --git a/spec/analytical/modules/mixpanel_spec.rb b/spec/analytical/modules/mixpanel_spec.rb index 75319ae..e77912e 100644 --- a/spec/analytical/modules/mixpanel_spec.rb +++ b/spec/analytical/modules/mixpanel_spec.rb @@ -27,17 +27,17 @@ 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