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 +4 -4
- data/example/example.rb +23 -8
- data/lib/rudder_analytics_sync/batch.rb +1 -1
- data/lib/rudder_analytics_sync/operations/identify.rb +3 -1
- data/lib/rudder_analytics_sync/operations/operation.rb +18 -13
- data/lib/rudder_analytics_sync/operations/page.rb +1 -1
- data/lib/rudder_analytics_sync/operations/track.rb +1 -1
- data/lib/rudder_analytics_sync/utils.rb +42 -10
- data/lib/rudder_analytics_sync/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e0cedeea6bd334e5d9740396e2076f2d7562acd7
|
4
|
+
data.tar.gz: d4d84573ed8d38feb37addf83f2d1f63d063a9b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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: '
|
9
|
-
data_plane_url: 'https://
|
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
|
-
|
17
|
-
|
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[:
|
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:
|
46
|
+
anonymousId: anonymous_id,
|
36
47
|
context: context,
|
37
|
-
integrations: options[:integrations],
|
38
|
-
timestamp:
|
39
|
-
sentAt: current_time
|
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
|
@@ -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
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
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
|