Commit 63f4b158 authored by Giorgenes Gelatti's avatar Giorgenes Gelatti Committed by Michael Kozono

Adds event tracker helper for controllers

Adds controller concern to track events
inside controller.
Allows to call #track_event() to track
arbitrary events.
parent 0f49eeb4
......@@ -13,6 +13,7 @@ class ApplicationController < ActionController::Base
include WithPerformanceBar
include SessionlessAuthentication
include ConfirmEmailWarning
include Gitlab::Tracking::ControllerConcern
before_action :authenticate_user!
before_action :enforce_terms!, if: :should_enforce_terms?
......
......@@ -6,6 +6,17 @@ module Gitlab
module Tracking
SNOWPLOW_NAMESPACE = 'gl'
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
end
class << self
def enabled?
Gitlab::CurrentSettings.snowplow_enabled?
......
......@@ -56,6 +56,8 @@ describe ApplicationController do
end
end
it_behaves_like 'a Trackable Controller'
describe '#add_gon_variables' do
before do
Gon.clear
......
# frozen_string_literal: true
shared_examples 'a Trackable Controller' do
describe '#track_event' do
before do
sign_in user
end
context 'with no params' do
controller(described_class) do
def index
track_event
head :ok
end
end
it 'tracks the action name' do
expect(Gitlab::Tracking).to receive(:event).with('AnonymousController', 'index', {})
get :index
end
end
context 'with params' do
controller(described_class) do
def index
track_event('some_event', category: 'SomeCategory', label: 'errorlabel')
head :ok
end
end
it 'tracks with the specified param' do
expect(Gitlab::Tracking).to receive(:event).with('SomeCategory', 'some_event', label: 'errorlabel')
get :index
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment