-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #200 from swishjam/instrumentation-improvements
instrumentation improvements
- Loading branch information
Showing
56 changed files
with
1,614 additions
and
725 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 0 additions & 119 deletions
119
backend/app/lib/ingestion/event_preparers/basic_event_handler.rb
This file was deleted.
Oops, something went wrong.
33 changes: 33 additions & 0 deletions
33
backend/app/lib/ingestion/event_preparers/helpers/automatic_user_attribute_applier.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
module Ingestion | ||
module EventPreparers | ||
module Helpers | ||
class AutomaticUserAttributeApplier | ||
AUTO_APPLY_USER_PROPERTIES_DICT = { | ||
# .URL and .REFERRER are legacy property names but falling back to these values for backwards compatibility | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_LANDING_PAGE_URL => [Analytics::Event::ReservedPropertyNames.SESSION_LANDING_PAGE_URL, Analytics::Event::ReservedPropertyNames.URL], | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_REFERRER_URL => [Analytics::Event::ReservedPropertyNames.SESSION_REFERRER_URL, Analytics::Event::ReservedPropertyNames.REFERRER], | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_UTM_CAMPAIGN => Analytics::Event::ReservedPropertyNames.SESSION_UTM_CAMPAIGN, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_UTM_SOURCE => Analytics::Event::ReservedPropertyNames.SESSION_UTM_SOURCE, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_UTM_MEDIUM => Analytics::Event::ReservedPropertyNames.SESSION_UTM_MEDIUM, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_UTM_CONTENT => Analytics::Event::ReservedPropertyNames.SESSION_UTM_CONTENT, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_UTM_TERM => Analytics::Event::ReservedPropertyNames.SESSION_UTM_TERM, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_GCLID => Analytics::Event::ReservedPropertyNames.SESSION_GCLID, | ||
} | ||
|
||
def self.apply_user_attributes_if_necessary!(user_properties, event_properties) | ||
AUTO_APPLY_USER_PROPERTIES_DICT.each do |user_property_key, event_property| | ||
next if user_properties.key?(user_property_key) | ||
user_property_value = nil | ||
if event_property.is_a?(Array) | ||
user_property_value = event_property.map{ |prop_name| event_properties[prop_name] }.compact.first | ||
else | ||
user_property_value = event_properties[event_property] | ||
end | ||
next if user_property_value.nil? | ||
user_properties[user_property_key] = user_property_value | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
71 changes: 71 additions & 0 deletions
71
backend/app/lib/ingestion/event_preparers/helpers/swishjam_event_organization_attributor.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
module Ingestion | ||
module EventPreparers | ||
module Helpers | ||
class SwishjamEventOrganizationAttributor < Ingestion::EventPreparers::Base | ||
attr_reader :parsed_event, :user_profile_for_event | ||
|
||
AUTO_APPLY_FROM_USER_PROPERTIES_DICT = [ | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_LANDING_PAGE_URL, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_REFERRER_URL, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_UTM_CAMPAIGN, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_UTM_SOURCE, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_UTM_MEDIUM, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_UTM_CONTENT, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_UTM_TERM, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_GCLID, | ||
] | ||
|
||
def initialize(parsed_event, user_profile_for_event) | ||
@parsed_event = parsed_event | ||
@user_profile_for_event = user_profile_for_event | ||
end | ||
|
||
def organization_for_event | ||
@organization_for_event ||= begin | ||
organization_identifier = org_attr('id', 'identifier', 'organization_identifier', 'org_id', 'organization_id', 'orgIdentifier', 'organizationIdentifier', 'organization_identifier') | ||
return if organization_identifier.blank? | ||
org = workspace.analytics_organization_profiles.find_by(organization_unique_identifier: organization_identifier) || workspace.analytics_organization_profiles.new(organization_unique_identifier: organization_identifier) | ||
maybe_org_name = org_attr('organization_name', 'name', 'organizationName', 'org_name', 'orgName') | ||
org.name = maybe_org_name if maybe_org_name.present? | ||
org.metadata ||= {} | ||
org.metadata = org.metadata.merge(sanitized_provided_org_properties) | ||
org.domain = org_attr('domain') if org_attr('domain').present? | ||
if org.domain.nil? && user_profile_for_event&.email.present? && !GenericEmailDetector.is_generic_email?(user_profile_for_event.email) | ||
domain_from_user_email = user_profile_for_event.email.split('@').last | ||
org.domain = domain_from_user_email | ||
end | ||
if user_profile_for_event.present? | ||
AUTO_APPLY_FROM_USER_PROPERTIES_DICT.each do |property_name| | ||
org.metadata[property_name] ||= user_profile_for_event.metadata[property_name] if user_profile_for_event.metadata[property_name].present? | ||
end | ||
end | ||
org.save! if org.changed? | ||
if user_profile_for_event.present? && !org.analytics_organization_members.exists?(analytics_user_profile_id: user_profile_for_event.id) | ||
org.analytics_organization_members.create!(analytics_user_profile_id: user_profile_for_event.id) | ||
end | ||
org | ||
end | ||
end | ||
|
||
def provided_org_attributes | ||
# legacy instrumentation sends `organization` events with the organization properties in the root of the event properties, and all other events with the organization properties in the `organization_attributes` key | ||
parsed_event.properties['organization'] || parsed_event.properties['organization_attributes'] || (parsed_event.name == 'organization' ? parsed_event.properties : {}) || {} | ||
end | ||
|
||
def org_attr(*keys) | ||
value = nil | ||
keys.each do |key| | ||
value = provided_org_attributes[key] || provided_org_attributes.dig('metadata', key) | ||
break if value.present? | ||
end | ||
value | ||
end | ||
|
||
def sanitized_provided_org_properties | ||
(provided_org_attributes['metadata'] || provided_org_attributes).except('id', 'identifier', 'organization_identifier', 'name', 'organization_name', 'domain', 'org_id', 'organization_id', 'orgIdentifier', 'organizationIdentifier', 'organization_identifier', *Analytics::Event::ReservedPropertyNames.all.map(&:to_s)) | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.