Class: LeanplumApi::API
- Inherits:
-
Object
- Object
- LeanplumApi::API
- Defined in:
- lib/leanplum_api/api.rb
Direct Known Subclasses
Constant Summary collapse
- SET_USER_ATTRIBUTES =
API Command Constants
'setUserAttributes'.freeze
- SET_DEVICE_ATTRIBUTES =
'setDeviceAttributes'.freeze
- TRACK =
'track'.freeze
- EXPORT_PENDING =
Data export related constants
'PENDING'.freeze
- EXPORT_RUNNING =
'RUNNING'.freeze
- EXPORT_FINISHED =
'FINISHED'.freeze
Instance Method Summary collapse
- #delete_user(user_id) ⇒ Object
- #export_user(user_id) ⇒ Object
- #get_ab_test(ab_test_id) ⇒ Object
- #get_ab_tests(only_recent = false) ⇒ Object
- #get_message(message_id) ⇒ Object
- #get_messages(only_recent = false) ⇒ Object
- #get_variant(variant_id) ⇒ Object
- #get_vars(user_id) ⇒ Object
- #import_csv(bucket, file_path, user_attributes = true) ⇒ Object
-
#initialize ⇒ API
constructor
A new instance of API.
-
#reset_anomalous_users(user_ids) ⇒ Object
If you pass old events OR users with old date attributes (e.g. create_date for an old user), Leanplum wil mark them ‘anomalous’ and exclude them from your data set.
- #set_device_attributes(device_attributes, options = {}) ⇒ Object
- #set_user_attributes(user_attributes, options = {}) ⇒ Object
- #track_events(events, options = {}) ⇒ Object
-
#track_multi(events: nil, user_attributes: nil, device_attributes: nil, options: {}) ⇒ Object
This method is for tracking events and/or updating user and/or device attributes at the same time, batched together like leanplum recommends.
- #user_attributes(user_id) ⇒ Object
- #user_events(user_id) ⇒ Object
Constructor Details
#initialize ⇒ API
Returns a new instance of API.
13 14 15 |
# File 'lib/leanplum_api/api.rb', line 13 def initialize fail 'LeanplumApi not configured yet!' unless LeanplumApi.configuration end |
Instance Method Details
#delete_user(user_id) ⇒ Object
87 88 89 |
# File 'lib/leanplum_api/api.rb', line 87 def delete_user(user_id) development_connection.get(action: 'deleteUser', userId: user_id).first['vars'] end |
#export_user(user_id) ⇒ Object
57 58 59 60 61 |
# File 'lib/leanplum_api/api.rb', line 57 def export_user(user_id) response = data_export_connection.get(action: 'exportUser', userId: user_id).first fail ResourceNotFoundError, "User #{user_id} not found" unless response['events'] || response['userAttributes'] response end |
#get_ab_test(ab_test_id) ⇒ Object
67 68 69 |
# File 'lib/leanplum_api/api.rb', line 67 def get_ab_test(ab_test_id) content_read_only_connection.get(action: 'getAbTest', id: ab_test_id).first['abTest'] end |
#get_ab_tests(only_recent = false) ⇒ Object
63 64 65 |
# File 'lib/leanplum_api/api.rb', line 63 def get_ab_tests(only_recent = false) content_read_only_connection.get(action: 'getAbTests', recent: only_recent).first['abTests'] end |
#get_message(message_id) ⇒ Object
79 80 81 |
# File 'lib/leanplum_api/api.rb', line 79 def () content_read_only_connection.get(action: 'getMessage', id: ).first['message'] end |
#get_messages(only_recent = false) ⇒ Object
75 76 77 |
# File 'lib/leanplum_api/api.rb', line 75 def (only_recent = false) content_read_only_connection.get(action: 'getMessages', recent: only_recent).first['messages'] end |
#get_variant(variant_id) ⇒ Object
71 72 73 |
# File 'lib/leanplum_api/api.rb', line 71 def get_variant(variant_id) content_read_only_connection.get(action: 'getVariant', id: variant_id).first['variant'] end |
#get_vars(user_id) ⇒ Object
83 84 85 |
# File 'lib/leanplum_api/api.rb', line 83 def get_vars(user_id) production_connection.get(action: 'getVars', userId: user_id).first['vars'] end |
#import_csv(bucket, file_path, user_attributes = true) ⇒ Object
100 101 102 103 104 105 106 107 108 |
# File 'lib/leanplum_api/api.rb', line 100 def import_csv(bucket, file_path, user_attributes = true) request_data = { createJob: true, defaultAction: user_attributes ? SET_USER_ATTRIBUTES : SET_DEVICE_ATTRIBUTES, gcsBucket: bucket, file: file_path } development_connection.multi_dev(request_data) end |
#reset_anomalous_users(user_ids) ⇒ Object
If you pass old events OR users with old date attributes (e.g. create_date for an old user), Leanplum wil mark them ‘anomalous’ and exclude them from your data set. Calling this method after you pass old events will fix that for all events for the specified user_id.
94 95 96 97 98 |
# File 'lib/leanplum_api/api.rb', line 94 def reset_anomalous_users(user_ids) user_ids = Array.wrap(user_ids) request_data = user_ids.map { |user_id| { action: SET_USER_ATTRIBUTES, resetAnomalies: true, userId: user_id } } development_connection.multi(request_data) end |
#set_device_attributes(device_attributes, options = {}) ⇒ Object
21 22 23 |
# File 'lib/leanplum_api/api.rb', line 21 def set_device_attributes(device_attributes, = {}) track_multi(device_attributes: device_attributes, options: ) end |
#set_user_attributes(user_attributes, options = {}) ⇒ Object
17 18 19 |
# File 'lib/leanplum_api/api.rb', line 17 def set_user_attributes(user_attributes, = {}) track_multi(user_attributes: user_attributes, options: ) end |
#track_events(events, options = {}) ⇒ Object
25 26 27 |
# File 'lib/leanplum_api/api.rb', line 25 def track_events(events, = {}) track_multi(events: events, options: ) end |
#track_multi(events: nil, user_attributes: nil, device_attributes: nil, options: {}) ⇒ Object
This method is for tracking events and/or updating user and/or device attributes at the same time, batched together like leanplum recommends. Set the :force_anomalous_override option to catch warnings from leanplum about anomalous events and force them to not be considered anomalous.
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/leanplum_api/api.rb', line 33 def track_multi(events: nil, user_attributes: nil, device_attributes: nil, options: {}) events = Array.wrap(events) request_data = events.map { |h| build_event_attributes_hash(h.dup, ) } + Array.wrap(user_attributes).map { |h| build_user_attributes_hash(h.dup) } + Array.wrap(device_attributes).map { |h| build_device_attributes_hash(h.dup) } response = production_connection.multi(request_data) force_anomalous_override(response, events) if [:force_anomalous_override] response end |
#user_attributes(user_id) ⇒ Object
46 47 48 49 50 51 |
# File 'lib/leanplum_api/api.rb', line 46 def user_attributes(user_id) # Leanplum returns strings instead of booleans export_user(user_id)['userAttributes'] .map { |k, v| [k, v.to_s =~ /\A(true|false)\z/i ? eval(v.downcase) : v] } .to_h end |
#user_events(user_id) ⇒ Object
53 54 55 |
# File 'lib/leanplum_api/api.rb', line 53 def user_events(user_id) export_user(user_id)['events'] end |