module Carto module Tracking module Formats class Segment def initialize(user: nil, visualization: nil, widget: nil, hash: {}) @user = user @visualization = visualization @widget = widget @connection = hash[:connection] @origin = hash[:origin] @page = hash[:page] @quota_overage = hash[:quota_overage] @mapviews = hash[:mapviews] @analysis = hash[:analysis] # add anything else as it arrives # add new properties in required_properties in events.rb for validation @others = hash.symbolize_keys.except(:visualization_id, :user_id, :widget_id, :connection, :origin, :page, :quota_overage, :mapviews, :analysis) end def to_hash properties = event_properties properties.merge!(user_properties) if @user properties.merge!(visualization_properties) if @visualization properties.merge!(connection_properties) if @connection properties.merge!(trending_map_properties) if @mapviews properties.merge!(analysis_properties) if @analysis properties.merge!(widget_properties) if @widget properties.merge!(@others) if @others properties[:page] = @page if @page properties[:quota_overage] = @quota_overage if @quota_overage properties end private def visualization_properties lifetime_in_days_with_decimals = days_with_decimals(now - @visualization.created_at) properties = { vis_id: @visualization.id, privacy: @visualization.privacy, type: @visualization.type, object_created_at: @visualization.created_at, lifetime: lifetime_in_days_with_decimals } properties[:origin] = @origin if @origin properties end def user_properties user_age_in_days_with_decimals = days_with_decimals(now - @user.created_at) { event_user_id: @user.id, event_origin: @user.builder_enabled? ? 'Builder' : 'Editor', plan: @user.account_type, user_active_for: user_age_in_days_with_decimals, user_created_at: @user.created_at, organization: @user.organization&.name } end def connection_properties { data_from: @connection[:data_from], imported_from: @connection[:imported_from], sync: @connection[:sync] || false, file_type: @connection[:file_type] } end def analysis_properties { analysis_id: @analysis[:id], analysis_natural_id: @analysis[:natural_id], analysis_type: @analysis[:type] } end def trending_map_properties { map_id: @visualization.id, map_name: @visualization.name, mapviews: @mapviews } end def widget_properties { widget_type: @widget.type } end def event_properties { creation_time: now } end def days_with_decimals(time_object) time_object.to_f / 60 / 60 / 24 end def now @now ||= Time.now.utc end end end end end