cheddargetter_client_ruby 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{cheddargetter_client_ruby}
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Expected Behavior"]
12
- s.date = %q{2010-12-11}
12
+ s.date = %q{2011-01-13}
13
13
  s.description = %q{A CheddarGetter API wrapper for Ruby}
14
14
  s.email = %q{support@expectedbehavior.com}
15
15
  s.extra_rdoc_files = [
@@ -120,7 +120,27 @@ module CheddarGetter
120
120
  # }
121
121
  # }
122
122
  #}
123
- def new_customer(data = { })
123
+ #
124
+ #Pass in the cookie info hash if you have been using the
125
+ #set_marketing_cookie method to track marketing metrics.
126
+ #Info from the marketing cookie will be passed along to
127
+ #Cheddar Getter in the new_customer call.
128
+ #
129
+ #cookie_info (optional):
130
+ #
131
+ #{
132
+ # :cookies => required
133
+ # :cookie_name => not_required
134
+ #}
135
+ def new_customer(data = { }, cookie_info = nil)
136
+ if cookie_info
137
+ cookie_name = cookie_info[:cookie_name] || DEFAULT_COOKIE_NAME
138
+ cookie_data = (YAML.load(cookie_info[:cookies][cookie_name] || "") rescue nil) || { }
139
+ [:firstContactDatetime, :referer, :campaignTerm, :campaignName,
140
+ :campaignSource, :campaignMedium, :campaignContent].each do |key|
141
+ data[key] ||= cookie_data[key] if cookie_data[key]
142
+ end
143
+ end
124
144
  do_request(:item => :customers, :action => :new, :data => data)
125
145
  end
126
146
 
@@ -303,6 +323,138 @@ module CheddarGetter
303
323
  do_request(:item => :customers, :action => "add-charge", :id_hash => id_hash, :data => data)
304
324
  end
305
325
 
326
+ #http://support.cheddargetter.com/faqs/marketing-metrics/marketing-metrics
327
+ #
328
+ #Convenience wrapper of setcookie() for setting a persistent cookie
329
+ #containing marketing metrics compatible with CheddarGetter's marketing metrics tracking.
330
+ #Running this method on every request to your marketing site sets or refines the marketing
331
+ #cookie data over time.
332
+ #If you are using this method, you can pass in the cookies to the new_customer call,
333
+ #which will automatically add the data to the customer record.
334
+ #
335
+ #Sample usage for your controller:
336
+ #
337
+ # before_filter :update_cheddar_getter_cookie
338
+ # def update_cheddar_getter_cookie
339
+ # CheddarGetter::Client.set_marketing_cookie(:cookies => cookies, :request => request)
340
+ # end
341
+ #
342
+ #options:
343
+ #
344
+ #{
345
+ # :cookies => required,
346
+ # :request => required,
347
+ # :cookie_name => not_required (default 'CGMK'),
348
+ # :expires => not_required (default 2 years),
349
+ # :path => not_required (default '/'),
350
+ # :domain => not_required (default nil),
351
+ # :secure => not_required (default false),
352
+ # :httponly => not_required (default false)
353
+ #}
354
+ def self.set_marketing_cookie(options = { })
355
+ default_options = {
356
+ :cookie_name => DEFAULT_COOKIE_NAME,
357
+ :expires => Time.now + 60*60*24*365*2,
358
+ :path => '/',
359
+ :domain => nil,
360
+ :secure => false,
361
+ :httponly => false
362
+ }
363
+
364
+ options = default_options.merge(options)
365
+ cookies = options[:cookies]
366
+ raise CheddarGetter::ClientException.new("The option :cookies is required") unless cookies
367
+ request = options[:request]
368
+ raise CheddarGetter::ClientException.new("The option :request is required") unless request
369
+
370
+ utm_params = {
371
+ :utm_term => :campaignTerm,
372
+ :utm_campaign => :campaignName,
373
+ :utm_source => :campaignSource,
374
+ :utm_medium => :campaignMedium,
375
+ :utm_content => :campaignContent
376
+ }
377
+
378
+ # get the existing cookie information, if any
379
+ cookie_data = (YAML.load(cookies[options[:cookie_name]] || "") rescue nil)
380
+ cookie_data = nil unless cookie_data.kind_of?(Hash)
381
+
382
+ # no cookie yet -- set the first contact date and referer in the cookie
383
+ # (only first request)
384
+ if !cookie_data
385
+ # when did this lead first find us? (right now!)
386
+ # we'll use this to determine the customer "vintage"
387
+ cookie_data = { :firstContactDatetime => Time.now.strftime("%Y-%m-%dT%H:%M:%S%z") }
388
+
389
+ # if there's a __utma cookie, we can get a more accurate time
390
+ # which helps us get better data from visitors who first found us
391
+ # before we started setting our own cookie
392
+ if cookies['__utma']
393
+ domain_hash, visitor_id, initial_visit, previous_visit, current_visit, visit_counter =
394
+ cookies['__utma'].split('.')
395
+
396
+ initial_visit = initial_visit.to_i
397
+ if initial_visit != 0
398
+ cookie_data[:firstContactDatetime] = Time.at(initial_visit).strftime("%Y-%m-%dT%H:%M:%S%z")
399
+ end
400
+ end
401
+
402
+ #set the raw referer (defaults to 'direct')
403
+ cookie_data[:referer] = 'direct'
404
+ cookie_data[:referer] = request.env['HTTP_REFERER'] unless request.env['HTTP_REFERER'].blank?
405
+
406
+ # if there's some utm vars
407
+ # When tagging your inbound links for google analytics
408
+ # http://www.google.com/support/analytics/bin/answer.py?answer=55518
409
+ # our cookie will also benenfit by the added params
410
+ utm_params.each { |k, v| cookie_data[v] = request.params[k] unless request.params[k].blank? }
411
+
412
+ cookies[options[:cookie_name]] = {
413
+ :value => cookie_data.to_yaml,
414
+ :path => options[:path],
415
+ :domain => options[:domain],
416
+ :expires => options[:expires],
417
+ :secure => options[:secure],
418
+ :httponly => options[:httponly]
419
+ }
420
+
421
+ # cookie is already set but maybe we can refine it with __utmz data
422
+ # (second and subsequent requests)
423
+ elsif cookies['__utmz']
424
+ return if cookie_data.size >= 3 #already has enough info
425
+
426
+ domain_hash, timestamp, session_number, campaign_number, campaign_data =
427
+ cookies['__utmz'].split('.')
428
+
429
+ return if campaign_data.blank?
430
+ campaign_data = (Hash[*campaign_data.split(/\||=/)] rescue { })
431
+
432
+ # see if it's a google adwords lead
433
+ # in this case, we only get the keyword
434
+ if ! campaign_data["utmgclid"].blank?
435
+ cookie_data[:campaignSource] = 'google';
436
+ cookie_data[:campaignMedium] = 'ppc';
437
+ cookie_data[:campaignTerm] = campaign_data["utmctr"] unless campaign_data["utmctr"].blank?
438
+ else
439
+ cookieData[:campaignSource] = campaign_data["utmcsr"] unless campaign_data["utmcsr"].blank?
440
+ cookieData[:campaignName] = campaign_data["utmccn"] unless campaign_data["utmccn"].blank?
441
+ cookieData[:campaignMedium] = campaign_data["utmcmd"] unless campaign_data["utmcmd"].blank?
442
+ cookieData[:campaignTerm] = campaign_data["utmctr"] unless campaign_data["utmctr"].blank?
443
+ cookieData[:campaignContent] = campaign_data["utmcct"] unless campaign_data["utmcct"].blank?
444
+ end
445
+
446
+ cookies[options[:cookie_name]] = {
447
+ :value => cookie_data.to_yaml,
448
+ :path => options[:path],
449
+ :domain => options[:domain],
450
+ :expires => options[:expires],
451
+ :secure => options[:secure],
452
+ :httponly => options[:httponly]
453
+ }
454
+ end
455
+ end
456
+
457
+
306
458
  private
307
459
  def get_identifier_string(type, id_hash)
308
460
  code = type ? "#{type}_code".to_sym : :code
@@ -356,9 +508,13 @@ module CheddarGetter
356
508
  :createdAfterDate => :year_month_day,
357
509
  :createdBeforeDate => :year_month_day,
358
510
  :canceledAfterDate => :year_month_day,
359
- :canceledBeforeDate => :year_month_day
511
+ :canceledBeforeDate => :year_month_day,
512
+ :firstContactDatetime => :datetime,
513
+ :changeBillDate => :datetime
360
514
  }
361
515
 
516
+ DEFAULT_COOKIE_NAME = 'CGMK'
517
+
362
518
  def deep_fix_request_data!(data)
363
519
  if data.is_a?(Array)
364
520
  data.each do |v|
@@ -372,7 +528,8 @@ module CheddarGetter
372
528
  data[k] = case type
373
529
  when :month_year then v.respond_to?(:strftime) ? v.strftime("%m/%Y") : v
374
530
  when :boolean then v ? "1" : "0"
375
- when :year_month_day then v.respond_to?(:strftime) ? v.strftime("%Y/%m/%d") : v
531
+ when :year_month_day then v.respond_to?(:strftime) ? v.strftime("%Y-%m-%d") : v
532
+ when :datetime then v.respond_to?(:strftime) ? v.strftime("%Y-%m-%dT%H:%M:%S%z") : v
376
533
  else v
377
534
  end
378
535
  end
@@ -171,7 +171,7 @@ module CheddarGetter
171
171
  #
172
172
  #code must be provided if this response contains more than one customer.
173
173
  def customer_outstanding_invoices(code = nil)
174
- now = DateTime.now
174
+ now = Time.now
175
175
  customer_invoices(code).reject do |i|
176
176
  i[:paidTransactionId] || i[:billingDatetime] > now
177
177
  end
@@ -282,7 +282,7 @@ module CheddarGetter
282
282
  data[k] = case type
283
283
  when :integer then v.to_i
284
284
  when :float then v.to_f
285
- when :datetime then DateTime.parse(v) rescue v
285
+ when :datetime then Time.parse(v) rescue v
286
286
  when :date then Date.parse(v) rescue v
287
287
  when :boolean then v.to_i != 0
288
288
  else v
@@ -40,6 +40,7 @@ class TestCheddargetterClientRuby < Test::Unit::TestCase
40
40
  :firstName => "First",
41
41
  :lastName => "Last",
42
42
  :email => "email@example.com",
43
+ :firstContactDatetime => Time.now,
43
44
  :subscription => {
44
45
  :planCode => "TEST_PLAN_2",
45
46
  :ccNumber => "4111111111111111",
@@ -873,4 +874,109 @@ class TestCheddargetterClientRuby < Test::Unit::TestCase
873
874
  assert_equal ["Credit card type is not accepted"], result.error_messages
874
875
  end
875
876
 
877
+ should "test setting the marketing cookie" do
878
+ cn = CheddarGetter::Client::DEFAULT_COOKIE_NAME
879
+ fake_request_class = Struct.new(:env, :params)
880
+ assert_raises(CheddarGetter::ClientException) { CheddarGetter::Client.set_marketing_cookie }
881
+ assert_raises(CheddarGetter::ClientException) { CheddarGetter::Client.set_marketing_cookie(:cookies => { }) }
882
+
883
+ request = fake_request_class.new({'HTTP_REFERER' => 'woot'}, { :utm_term => 'hi' })
884
+ time = Time.now
885
+ cookies = { "__utma" => " . .#{time.to_i}. . . "}
886
+ CheddarGetter::Client.set_marketing_cookie(:cookies => cookies, :request => request)
887
+
888
+ assert_equal '/', cookies[cn][:path]
889
+ assert_equal nil, cookies[cn][:domain]
890
+ assert_equal false, cookies[cn][:secure]
891
+ assert_equal false, cookies[cn][:httponly]
892
+ cookie_data = YAML.load(cookies[cn][:value])
893
+ assert_equal 3, cookie_data.size
894
+ assert_equal time.strftime("%Y-%m-%dT%H:%M:%S%z"), cookie_data[:firstContactDatetime]
895
+ assert_equal "woot", cookie_data[:referer]
896
+ assert_equal "hi", cookie_data[:campaignTerm]
897
+
898
+ cookies[cn] = cookies[cn][:value]
899
+ cookies['__utmz'] = " . . . .utmgclid=value|utmctr=bye"
900
+ CheddarGetter::Client.set_marketing_cookie(:cookies => cookies, :request => request)
901
+ cookie_data = YAML.load(cookies[cn]) #was not manipulated
902
+ assert_equal 3, cookie_data.size
903
+ assert_equal time.strftime("%Y-%m-%dT%H:%M:%S%z"), cookie_data[:firstContactDatetime]
904
+ assert_equal "woot", cookie_data[:referer]
905
+ assert_equal "hi", cookie_data[:campaignTerm]
906
+
907
+ cookie_data.delete(:campaignTerm)
908
+ cookies[cn] = cookie_data.to_yaml
909
+ CheddarGetter::Client.set_marketing_cookie(:cookies => cookies, :request => request)
910
+ cookie_data = YAML.load(cookies[cn][:value])
911
+ assert_equal 5, cookie_data.size
912
+ assert_equal time.strftime("%Y-%m-%dT%H:%M:%S%z"), cookie_data[:firstContactDatetime]
913
+ assert_equal "woot", cookie_data[:referer]
914
+ assert_equal "bye", cookie_data[:campaignTerm]
915
+ assert_equal "google", cookie_data[:campaignSource]
916
+ assert_equal "ppc", cookie_data[:campaignMedium]
917
+
918
+ cookie_data.delete(:campaignTerm)
919
+ cookie_data.delete(:campaignSource)
920
+ cookie_data.delete(:campaignMedium)
921
+ cookies['__utmz'] = " . . . ."
922
+ cookies[cn] = cookie_data.to_yaml
923
+ CheddarGetter::Client.set_marketing_cookie(:cookies => cookies, :request => request)
924
+ cookie_data = YAML.load(cookies[cn]) #was not manipulated
925
+ assert_equal 2, cookie_data.size
926
+ assert_equal time.strftime("%Y-%m-%dT%H:%M:%S%z"), cookie_data[:firstContactDatetime]
927
+ assert_equal "woot", cookie_data[:referer]
928
+
929
+ cookies['__utmz'] = " . . . .utmgclid=value"
930
+ cookies[cn] = cookie_data.to_yaml
931
+ CheddarGetter::Client.set_marketing_cookie(:cookies => cookies, :request => request)
932
+ cookie_data = YAML.load(cookies[cn][:value])
933
+ assert_equal 4, cookie_data.size
934
+ assert_equal time.strftime("%Y-%m-%dT%H:%M:%S%z"), cookie_data[:firstContactDatetime]
935
+ assert_equal "woot", cookie_data[:referer]
936
+ assert_equal "google", cookie_data[:campaignSource]
937
+ assert_equal "ppc", cookie_data[:campaignMedium]
938
+
939
+ cookie_data.delete(:campaignTerm)
940
+ cookie_data.delete(:campaignSource)
941
+ cookie_data.delete(:campaignMedium)
942
+ cookies['__utmz'] = " . . . . "
943
+ cookies[cn] = cookie_data.to_yaml
944
+ CheddarGetter::Client.set_marketing_cookie(:cookies => cookies, :request => request)
945
+ cookie_data = YAML.load(cookies[cn][:value])
946
+ assert_equal 2, cookie_data.size
947
+ assert_equal time.strftime("%Y-%m-%dT%H:%M:%S%z"), cookie_data[:firstContactDatetime]
948
+ assert_equal "woot", cookie_data[:referer]
949
+
950
+ cookies[cn] = "stuff"
951
+ CheddarGetter::Client.set_marketing_cookie(:cookies => cookies, :request => request)
952
+ cookie_data = YAML.load(cookies[cn][:value])
953
+ assert_equal 3, cookie_data.size
954
+ assert_equal time.strftime("%Y-%m-%dT%H:%M:%S%z"), cookie_data[:firstContactDatetime]
955
+ assert_equal "woot", cookie_data[:referer]
956
+ assert_equal "hi", cookie_data[:campaignTerm]
957
+ end
958
+
959
+ should "create a customer with marketing data" do
960
+ result = CG.delete_all_customers
961
+ assert_equal true, result.valid?
962
+
963
+ time = Time.now
964
+
965
+ cookies = { CheddarGetter::Client::DEFAULT_COOKIE_NAME => ({
966
+ :firstContactDatetime => time.strftime("%Y-%m-%dT%H:%M:%S%z"),
967
+ :referer => 'direct',
968
+ :campaignTerm => "incorrect"
969
+ }.to_yaml) }
970
+
971
+ result = CG.new_customer({ :campaignTerm => "correct"}.merge(free_new_user_hash(1)),
972
+ { :cookies => cookies })
973
+ assert_equal 1, result.customers.size
974
+ assert_equal "1", result.customer[:code]
975
+ assert_equal "Free Plan Test", result.customer_plan[:name]
976
+ assert_equal time.strftime("%Y-%m-%dT%H:%M:%S%z"),
977
+ result.customer[:firstContactDatetime].strftime("%Y-%m-%dT%H:%M:%S%z")
978
+ assert_equal 'direct', result.customer[:referer]
979
+ assert_equal 'correct', result.customer[:campaignTerm]
980
+ end
981
+
876
982
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cheddargetter_client_ruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 1
10
- version: 0.2.1
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Expected Behavior
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-12-11 00:00:00 -05:00
18
+ date: 2011-01-13 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency