2019-08-28 02:52:14 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'snowplow-tracker'
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module Tracking
|
|
|
|
SNOWPLOW_NAMESPACE = 'gl'
|
|
|
|
|
2019-09-26 17:06:29 -04:00
|
|
|
module ControllerConcern
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def track_event(action = action_name, **args)
|
|
|
|
category = args.delete(:category) || self.class.name
|
|
|
|
Gitlab::Tracking.event(category, action.to_s, **args)
|
|
|
|
end
|
2019-10-11 05:06:43 -04:00
|
|
|
|
|
|
|
def track_self_describing_event(schema_url, event_data_json, **args)
|
|
|
|
Gitlab::Tracking.self_describing_event(schema_url, event_data_json, **args)
|
|
|
|
end
|
2019-09-26 17:06:29 -04:00
|
|
|
end
|
|
|
|
|
2019-08-28 02:52:14 -04:00
|
|
|
class << self
|
|
|
|
def enabled?
|
|
|
|
Gitlab::CurrentSettings.snowplow_enabled?
|
|
|
|
end
|
|
|
|
|
|
|
|
def event(category, action, label: nil, property: nil, value: nil, context: nil)
|
|
|
|
return unless enabled?
|
|
|
|
|
2019-12-03 07:06:34 -05:00
|
|
|
snowplow.track_struct_event(category, action, label, property, value, context, (Time.now.to_f * 1000).to_i)
|
2019-08-28 02:52:14 -04:00
|
|
|
end
|
|
|
|
|
2019-10-11 05:06:43 -04:00
|
|
|
def self_describing_event(schema_url, event_data_json, context: nil)
|
|
|
|
return unless enabled?
|
|
|
|
|
|
|
|
event_json = SnowplowTracker::SelfDescribingJson.new(schema_url, event_data_json)
|
2019-12-03 07:06:34 -05:00
|
|
|
snowplow.track_self_describing_event(event_json, context, (Time.now.to_f * 1000).to_i)
|
2019-10-11 05:06:43 -04:00
|
|
|
end
|
|
|
|
|
2019-10-24 17:06:26 -04:00
|
|
|
def snowplow_options(group)
|
2019-08-28 02:52:14 -04:00
|
|
|
additional_features = Feature.enabled?(:additional_snowplow_tracking, group)
|
|
|
|
{
|
|
|
|
namespace: SNOWPLOW_NAMESPACE,
|
|
|
|
hostname: Gitlab::CurrentSettings.snowplow_collector_hostname,
|
|
|
|
cookie_domain: Gitlab::CurrentSettings.snowplow_cookie_domain,
|
2019-11-08 16:06:38 -05:00
|
|
|
app_id: Gitlab::CurrentSettings.snowplow_app_id,
|
2019-09-18 10:02:45 -04:00
|
|
|
form_tracking: additional_features,
|
2020-09-09 20:08:32 -04:00
|
|
|
link_click_tracking: additional_features
|
2019-08-28 02:52:14 -04:00
|
|
|
}.transform_keys! { |key| key.to_s.camelize(:lower).to_sym }
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def snowplow
|
|
|
|
@snowplow ||= SnowplowTracker::Tracker.new(
|
2020-09-03 14:08:29 -04:00
|
|
|
emitter,
|
2019-08-28 02:52:14 -04:00
|
|
|
SnowplowTracker::Subject.new,
|
|
|
|
SNOWPLOW_NAMESPACE,
|
2019-11-08 16:06:38 -05:00
|
|
|
Gitlab::CurrentSettings.snowplow_app_id
|
2019-08-28 02:52:14 -04:00
|
|
|
)
|
|
|
|
end
|
2020-09-03 14:08:29 -04:00
|
|
|
|
|
|
|
def emitter
|
|
|
|
SnowplowTracker::AsyncEmitter.new(
|
|
|
|
Gitlab::CurrentSettings.snowplow_collector_hostname,
|
|
|
|
protocol: 'https'
|
|
|
|
)
|
|
|
|
end
|
2019-08-28 02:52:14 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|