rudder_analytics_sync 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5726304487704b78e0e4af5710528e3b065b590a
4
- data.tar.gz: 33ff76d7d2fcec35e640908472061a683061afcd
3
+ metadata.gz: e0cedeea6bd334e5d9740396e2076f2d7562acd7
4
+ data.tar.gz: d4d84573ed8d38feb37addf83f2d1f63d063a9b2
5
5
  SHA512:
6
- metadata.gz: 53e11d8490d9184c53d791e8f38dbeb8c262137d4732ccc405a6e999bb2b52c6bf9396b881d2dc476380a364f3f9d36b286eee1687208ccb73feaf2ca56f681a
7
- data.tar.gz: 31790054a46185fcbdb96b721cdd58af13f687cf71d839ef36d6f6bb4fbe0215d6baf7d833e25a3db9bdd4e698c77cd593134e39e4dd4e5a79e33f103b2ace76
6
+ metadata.gz: dc32266bd0dcef218ccc61d3ebb443da965c4d14e74b877aeb1e4b3a733c63c0f204968ce730138a32ffa758834081ac1aa14192d26ab3eb9960ee5328e6457d
7
+ data.tar.gz: 5c1e78aa055457d7611ceecc1d59d051abe46227ae9998981084f7f429cbef8c3d04dab3a8c7e31760efdeaf8ec6f858fde5b666bfe7bf601bcf070b7924d7fa
data/example/example.rb CHANGED
@@ -1,29 +1,44 @@
1
- $LOAD_PATH << './lib/rudder_analytics_sync'
2
-
3
1
  require 'rudder_analytics_sync'
4
2
 
5
3
  include RudderAnalyticsSync
6
4
 
7
5
  analytics = RudderAnalyticsSync::Client.new(
8
- write_key: 'YOUR_WRITE_KEY', # required
9
- data_plane_url: 'https://a14c63e1.ngrok.io',
6
+ write_key: '1aJDXU7xlQpUp6qW1ppTJTvkgSi', # required
7
+ data_plane_url: 'https://86143ed0.ngrok.io',
10
8
  on_error: proc { |error_code, error_body, exception, response|
11
9
  # defaults to an empty proc
12
10
  }
13
11
  )
14
12
 
15
- analytics.track(
16
- user_id: 'test_user_id',
17
- event: 'Created Account'
18
- )
13
+ # analytics.track(
14
+ # user_id: 'test_user_id',
15
+ # event: 'Created Account'
16
+ # )
19
17
 
20
18
  analytics.batch do |batch|
21
19
  batch.track(
22
20
  user_id: 'test_user_id',
23
21
  event: 'Created Account'
24
22
  )
23
+ batch.page(
24
+ user_id: 'test_user_id',
25
+ name: 'Created Account',
26
+ properties: {
27
+ k1: 'v1'
28
+ }
29
+ )
25
30
  batch.track(
26
31
  user_id: 'test_user_id',
27
32
  event: 'Closed Account'
28
33
  )
34
+ batch.identify(
35
+ user_id: 'test_user_id_1',
36
+ traits: {
37
+ name: 'test'
38
+ }
39
+ )
40
+ batch.group(
41
+ user_id: 'test_user_id',
42
+ group_id: 'group_id'
43
+ )
29
44
  end
@@ -56,7 +56,7 @@ module RudderAnalyticsSync
56
56
  def add(operation_class, options, action)
57
57
  operation = operation_class.new(client, symbolize_keys(options))
58
58
  operation_payload = operation.build_payload
59
- operation_payload[:action] = action
59
+ operation_payload[:type] = action
60
60
  payload[:batch] << operation_payload
61
61
  end
62
62
  end
@@ -8,9 +8,11 @@ module RudderAnalyticsSync
8
8
  end
9
9
 
10
10
  def build_payload
11
- base_payload.merge(
11
+ merged_payload = base_payload.merge(
12
12
  traits: options[:traits] && isoify_dates!(options[:traits])
13
13
  )
14
+ merged_payload[:context][:traits] = merged_payload[:context][:traits].merge(options[:traits])
15
+ merged_payload
14
16
  end
15
17
  end
16
18
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'rudder_analytics_sync./utils'
4
+
3
5
  module RudderAnalyticsSync
4
6
  module Operations
5
7
  class Operation
@@ -9,6 +11,9 @@ module RudderAnalyticsSync
9
11
  library: {
10
12
  name: 'rudder-sdk-ruby-sync',
11
13
  version: RudderAnalyticsSync::VERSION
14
+ },
15
+ traits: {
16
+
12
17
  }
13
18
  }.freeze
14
19
 
@@ -28,15 +33,23 @@ module RudderAnalyticsSync
28
33
 
29
34
  def base_payload
30
35
  check_identity!
31
- current_time = Time.now
36
+ current_time = Time.now.utc
37
+
38
+ anonymous_id = options[:anonymous_id] || uid()
39
+ context[:traits] = (context[:traits] || {}) && {
40
+ anonymousId: anonymous_id,
41
+ userId: options[:user_id]
42
+ }
32
43
 
33
44
  {
34
45
  userId: options[:user_id],
35
- anonymousId: options[:anonymous_id],
46
+ anonymousId: anonymous_id,
36
47
  context: context,
37
- integrations: options[:integrations],
38
- timestamp: timestamp(options.fetch(:timestamp, current_time)),
39
- sentAt: current_time.iso8601
48
+ integrations: options[:integrations] || {All: true},
49
+ timestamp: maybe_datetime_in_iso8601(options[:timestamp] || Time.now.utc),
50
+ sentAt: maybe_datetime_in_iso8601(current_time),
51
+ messageId: uid(),
52
+ properties: options[:properties] || {}
40
53
  }
41
54
  end
42
55
 
@@ -44,14 +57,6 @@ module RudderAnalyticsSync
44
57
  raise ArgumentError, 'user_id or anonymous_id must be present' \
45
58
  unless options[:user_id] || options[:anonymous_id]
46
59
  end
47
-
48
- def timestamp(timestamp)
49
- if timestamp.respond_to?(:iso8601)
50
- timestamp.iso8601
51
- else
52
- Time.iso8601(timestamp).iso8601
53
- end
54
- end
55
60
  end
56
61
  end
57
62
  end
@@ -12,7 +12,7 @@ module RudderAnalyticsSync
12
12
 
13
13
  base_payload.merge(
14
14
  name: options[:name],
15
- properties: properties
15
+ properties: properties || {}
16
16
  )
17
17
  end
18
18
  end
@@ -15,7 +15,7 @@ module RudderAnalyticsSync
15
15
 
16
16
  base_payload.merge(
17
17
  event: options[:event],
18
- properties: properties
18
+ properties: properties || {}
19
19
  )
20
20
  end
21
21
  end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'securerandom'
4
+
3
5
  module RudderAnalyticsSync
4
6
  module Utils
5
7
  def self.included(klass)
@@ -27,17 +29,47 @@ module RudderAnalyticsSync
27
29
  end
28
30
  end
29
31
 
30
- def maybe_datetime_in_iso8601(prop)
31
- case prop
32
- when Time
33
- prop.iso8601(3)
34
- when DateTime
35
- prop.to_time.iso8601(3)
36
- when Date
37
- prop.strftime('%F')
38
- else
39
- prop
32
+ # public: Returns a uid string
33
+ #
34
+ def uid
35
+ arr = SecureRandom.random_bytes(16).unpack('NnnnnN')
36
+ arr[2] = (arr[2] & 0x0fff) | 0x4000
37
+ arr[3] = (arr[3] & 0x3fff) | 0x8000
38
+ '%08x-%04x-%04x-%04x-%04x%08x' % arr
39
+ end
40
+
41
+ def maybe_datetime_in_iso8601(datetime)
42
+ case datetime
43
+ when Time
44
+ time_in_iso8601 datetime
45
+ when DateTime
46
+ time_in_iso8601 datetime.to_time
47
+ when Date
48
+ date_in_iso8601 datetime
49
+ else
50
+ datetime
40
51
  end
41
52
  end
53
+
54
+ def time_in_iso8601(time, fraction_digits = 3)
55
+ fraction = (('.%06i' % time.usec)[0, fraction_digits + 1] if fraction_digits > 0)
56
+
57
+ "#{time.strftime('%Y-%m-%dT%H:%M:%S')}#{fraction}#{formatted_offset(time, true, 'Z')}"
58
+ end
59
+
60
+ def date_in_iso8601(date)
61
+ date.strftime('%F')
62
+ end
63
+
64
+ def formatted_offset(time, colon = true, alternate_utc_string = nil)
65
+ time.utc? && alternate_utc_string || seconds_to_utc_offset(time.utc_offset, colon)
66
+ end
67
+
68
+ def seconds_to_utc_offset(seconds, colon = true)
69
+ (colon ? UTC_OFFSET_WITH_COLON : UTC_OFFSET_WITHOUT_COLON) % [(seconds < 0 ? '-' : '+'), (seconds.abs / 3600), ((seconds.abs % 3600) / 60)]
70
+ end
71
+
72
+ UTC_OFFSET_WITH_COLON = '%s%02d:%02d'
73
+ UTC_OFFSET_WITHOUT_COLON = UTC_OFFSET_WITH_COLON.sub(':', '')
42
74
  end
43
75
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RudderAnalyticsSync
4
- VERSION = '1.0.0'
4
+ VERSION = '1.0.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rudder_analytics_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - RudderStack