-
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 #202 from swishjam/supplemented-augment-event-user…
…-properties supplement/augment event/user properties
- Loading branch information
Showing
15 changed files
with
418 additions
and
143 deletions.
There are no files selected for viewing
33 changes: 0 additions & 33 deletions
33
backend/app/lib/ingestion/event_preparers/helpers/automatic_user_attribute_applier.rb
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
backend/app/lib/ingestion/event_preparers/helpers/event_properties_augmentor.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,21 @@ | ||
module Ingestion | ||
module EventPreparers | ||
module Helpers | ||
class EventPropertiesAugmentor < Ingestion::EventPreparers::Base | ||
URL_PROPERTIES_TO_NORMALIZE = [ | ||
Analytics::Event::ReservedPropertyNames.URL, | ||
Analytics::Event::ReservedPropertyNames.PAGE_REFERRER, | ||
Analytics::Event::ReservedPropertyNames.SESSION_LANDING_PAGE_URL, | ||
Analytics::Event::ReservedPropertyNames.SESSION_REFERRER_URL, | ||
Analytics::Event::ReservedPropertyNames.REFERRER, # deprecated, but including to account for older SDKs that dont use PAGE_REFERRER | ||
] | ||
|
||
def augment_properties! | ||
URL_PROPERTIES_TO_NORMALIZE.each do |url_property_name| | ||
Ingestion::EventPreparers::Helpers::PropertyAugmentors::UrlNormalizer.add_normalized_url_properties!(parsed_event, url_property_name) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
53 changes: 53 additions & 0 deletions
53
backend/app/lib/ingestion/event_preparers/helpers/organization_properties_augmentor.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,53 @@ | ||
module Ingestion | ||
module EventPreparers | ||
module Helpers | ||
class OrganizationPropertiesAugmentor < Ingestion::EventPreparers::Base | ||
attr_reader :org, :parsed_event, :user_profile_for_event | ||
|
||
AUTO_APPLY_FROM_USER_PROPERTIES_DICT = [ | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_LANDING_PAGE_URL, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_LANDING_PAGE_URL_HOST, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_LANDING_PAGE_URL_PATH, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_LANDING_PAGE_URL_QUERY_PARAMS, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_LANDING_PAGE_BASE_URL, | ||
|
||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_REFERRER_URL, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_REFERRER_URL_HOST, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_REFERRER_URL_PATH, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_REFERRER_URL_QUERY_PARAMS, | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_REFERRER_BASE_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(org, parsed_event, user_profile_for_event) | ||
@org = org | ||
@parsed_event = parsed_event | ||
@user_profile_for_event = user_profile_for_event | ||
end | ||
|
||
def augment_organization_properties! | ||
return org if !user_profile_for_event.present? || org_has_any_initial_properties_set? | ||
AUTO_APPLY_FROM_USER_PROPERTIES_DICT.each do |property_name| | ||
next if !user_profile_for_event.metadata[property_name].present? | ||
org.metadata[property_name] ||= user_profile_for_event.metadata[property_name] | ||
end | ||
org.save! if org.changed? | ||
org | ||
end | ||
|
||
private | ||
|
||
def org_has_any_initial_properties_set? | ||
AUTO_APPLY_FROM_USER_PROPERTIES_DICT.any?{ |property_name| org.metadata.key?(property_name) } | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
34 changes: 34 additions & 0 deletions
34
backend/app/lib/ingestion/event_preparers/helpers/property_augmentors/url_normalizer.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,34 @@ | ||
module Ingestion | ||
module EventPreparers | ||
module Helpers | ||
module PropertyAugmentors | ||
class UrlNormalizer | ||
def self.add_normalized_url_properties!(parsed_event, property_name) | ||
return if !parsed_event.properties[property_name].present? | ||
return if parsed_event.properties[property_name] == 'direct' | ||
parsed_url = URI.parse(parsed_event.properties[property_name]) | ||
parsed_event.set_property(new_property_name(property_name, 'url_path'), parsed_url.path.blank? ? '/' : parsed_url.path) | ||
parsed_event.set_property(new_property_name(property_name, 'url_host'), parsed_url.host) | ||
parsed_event.set_property(new_property_name(property_name, 'url_query_params'), parsed_url.query) | ||
parsed_event.set_property(new_property_name(property_name, 'base_url'), "#{parsed_url.host}#{parsed_url.path == '/' ? '' : parsed_url.path}") | ||
parsed_event | ||
rescue URI::InvalidURIError => e | ||
Sentry.capture_exception(e, extra: { attempted_url: parsed_event.properties[property_name] }) | ||
parsed_event | ||
end | ||
|
||
def self.new_property_name(old_property_name, augmented_proprety_name) | ||
if old_property_name.include?('url') | ||
# session_landing_page_url -> session_landing_page_url_path | ||
old_property_name.gsub('url', augmented_proprety_name) | ||
else | ||
# referrer -> referrer_url_path | ||
[old_property_name, augmented_proprety_name].join('_') | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
end |
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
62 changes: 62 additions & 0 deletions
62
backend/app/lib/ingestion/event_preparers/helpers/user_properties_augmentor.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,62 @@ | ||
module Ingestion | ||
module EventPreparers | ||
module Helpers | ||
class UserPropertiesAugmentor < Ingestion::EventPreparers::Base | ||
attr_reader :user_profile, :parsed_event | ||
|
||
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_LANDING_PAGE_URL_HOST => [Analytics::Event::ReservedPropertyNames.SESSION_LANDING_PAGE_URL_HOST, Analytics::Event::ReservedPropertyNames.URL_HOST], | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_LANDING_PAGE_URL_PATH => [Analytics::Event::ReservedPropertyNames.SESSION_LANDING_PAGE_URL_PATH, Analytics::Event::ReservedPropertyNames.URL_PATH], | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_LANDING_PAGE_URL_QUERY_PARAMS => [Analytics::Event::ReservedPropertyNames.SESSION_LANDING_PAGE_URL_QUERY_PARAMS, Analytics::Event::ReservedPropertyNames.URL_QUERY_PARAMS], | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_LANDING_PAGE_BASE_URL => [Analytics::Event::ReservedPropertyNames.SESSION_LANDING_PAGE_BASE_URL, Analytics::Event::ReservedPropertyNames.BASE_URL], | ||
|
||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_REFERRER_URL => [Analytics::Event::ReservedPropertyNames.SESSION_REFERRER_URL, Analytics::Event::ReservedPropertyNames.REFERRER], | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_REFERRER_URL_HOST => [Analytics::Event::ReservedPropertyNames.SESSION_REFERRER_URL_HOST, Analytics::Event::ReservedPropertyNames.REFERRER_URL_HOST], | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_REFERRER_URL_PATH => [Analytics::Event::ReservedPropertyNames.SESSION_REFERRER_URL_PATH, Analytics::Event::ReservedPropertyNames.REFERRER_URL_PATH], | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_REFERRER_URL_QUERY_PARAMS => [Analytics::Event::ReservedPropertyNames.SESSION_REFERRER_URL_QUERY_PARAMS, Analytics::Event::ReservedPropertyNames.REFERRER_URL_QUERY_PARAMS], | ||
AnalyticsUserProfile::ReservedMetadataProperties.INITIAL_REFERRER_BASE_URL => [Analytics::Event::ReservedPropertyNames.SESSION_REFERRER_BASE_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 initialize(user_profile, parsed_event) | ||
@user_profile = user_profile | ||
@parsed_event = parsed_event | ||
end | ||
|
||
def augment_user_properties! | ||
# if we have already set the `initial_referrer_url`, but not the `initial_landing_page_url` then we should not continue | ||
# this is really only for users who have been identified earlier than ~04/22/2024 | ||
return user_profile if user_already_has_any_initial_properties_set? | ||
AUTO_APPLY_USER_PROPERTIES_DICT.each do |user_property_key, event_property_name| | ||
next if user_profile.metadata.key?(user_property_key) | ||
user_property_value = nil | ||
if event_property_name.is_a?(Array) | ||
user_property_value = event_property_name.map{ |prop_name| parsed_event.properties[prop_name] }.compact.first | ||
else | ||
user_property_value = parsed_event.properties[event_property_name] | ||
end | ||
next if user_property_value.nil? | ||
user_profile.metadata[user_property_key] = user_property_value | ||
end | ||
user_profile.save! if user_profile.changed? | ||
user_profile | ||
end | ||
|
||
private | ||
|
||
def user_already_has_any_initial_properties_set? | ||
AUTO_APPLY_USER_PROPERTIES_DICT.keys.any?{ |property_name| user_profile.metadata.key?(property_name) } | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |
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
Oops, something went wrong.