Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add uuid option to capture #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/posthog/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def initialize(opts = {})
@api_key,
opts[:host]
)

@distinct_id_has_sent_flag_calls = SizeLimitedHash.new(Defaults::MAX_HASH_SIZE) { |hash, key| hash[key] = Array.new }

at_exit { @worker_thread && @worker_thread[:should_exit] = true }
Expand Down Expand Up @@ -87,6 +87,7 @@ def clear
# @option attrs [String] :event Event name
# @option attrs [Hash] :properties Event properties (optional)
# @option attrs [Bool] :send_feature_flags Whether to send feature flags with this event (optional)
# @option attrs [String] :uuid ID that uniquely identifies an event
# @macro common_attrs
def capture(attrs)
symbolize_keys! attrs
Expand Down Expand Up @@ -160,7 +161,7 @@ def is_feature_enabled(flag_key, distinct_id, groups: {}, person_properties: {},
# @param [Hash] groups
# @param [Hash] person_properties key-value pairs of properties to associate with the user.
# @param [Hash] group_properties
#
#
# @return [String, nil] The value of the feature flag
#
# The provided properties are used to calculate feature flags locally, if possible.
Expand Down Expand Up @@ -202,7 +203,7 @@ def get_feature_flag(key, distinct_id, groups: {}, person_properties: {}, group_
# @param [Hash] groups
# @param [Hash] person_properties key-value pairs of properties to associate with the user.
# @param [Hash] group_properties
#
#
# @return [Hash] String (not symbol) key value pairs of flag and their values
def get_all_flags(distinct_id, groups: {}, person_properties: {}, group_properties: {}, only_evaluate_locally: false)
person_properties, group_properties = add_local_person_and_group_properties(distinct_id, groups, person_properties, group_properties)
Expand Down
13 changes: 13 additions & 0 deletions lib/posthog/field_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ class << self
# - "event"
# - "properties"
# - "groups"
# - "uuid"
def parse_for_capture(fields)
common = parse_common_fields(fields)

event = fields[:event]
properties = fields[:properties] || {}
groups = fields[:groups]
uuid = fields[:uuid]

check_presence!(event, 'event')
check_is_hash!(properties, 'properties')
Expand All @@ -23,6 +25,11 @@ def parse_for_capture(fields)
properties["$groups"] = groups
end

if uuid
check_is_uuid!(uuid)
common[:uuid] = uuid
end

isoify_dates! properties

common.merge(
Expand Down Expand Up @@ -164,6 +171,12 @@ def check_presence!(obj, name)
def check_is_hash!(obj, name)
raise ArgumentError, "#{name} must be a Hash" unless obj.is_a? Hash
end

def check_is_uuid!(uuid)
unless uuid.match?(/^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/i)
raise ArgumentError, "uuid is not formated like a uuid"
end
end
end
end
end
Loading