Commit 9b279308 authored by Ash McKenzie's avatar Ash McKenzie Committed by Martin Wortschack

Snowplow: setUserId to User.id if available

For gitlab.com only.

Snowplow: :individual_tracking feature

Add in new tracking module methodology
parent fc03eef3
......@@ -102,6 +102,7 @@ export function initUserTracking() {
window.snowplow('enableActivityTracking', 30, 30);
window.snowplow('trackPageView'); // must be after enableActivityTracking
if (opts.userId) window.snowplow('setUserId', opts.userId);
if (opts.formTracking) window.snowplow('enableFormTracking');
if (opts.linkClickTracking) window.snowplow('enableLinkClickTracking');
......
......@@ -7,4 +7,4 @@
};p[i].q=p[i].q||[];n=l.createElement(o);g=l.getElementsByTagName(o)[0];n.async=1;
n.src=w;g.parentNode.insertBefore(n,g)}}(window,document,"script","#{asset_url('snowplow/sp.js')}","snowplow"));
window.snowplowOptions = #{Gitlab::Tracking.snowplow_options(@group).to_json}
window.snowplowOptions = #{Gitlab::Tracking.snowplow_options(@group, current_user).to_json}
......@@ -39,7 +39,7 @@ module Gitlab
snowplow.track_self_describing_event(event_json, context, Time.now.to_i)
end
def snowplow_options(group)
def snowplow_options(group, user)
additional_features = Feature.enabled?(:additional_snowplow_tracking, group)
{
namespace: SNOWPLOW_NAMESPACE,
......@@ -47,7 +47,8 @@ module Gitlab
cookie_domain: Gitlab::CurrentSettings.snowplow_cookie_domain,
app_id: Gitlab::CurrentSettings.snowplow_site_id,
form_tracking: additional_features,
link_click_tracking: additional_features
link_click_tracking: additional_features,
user_id: user&.id
}.transform_keys! { |key| key.to_s.camelize(:lower).to_sym }
end
......
......@@ -11,6 +11,7 @@ describe('Tracking', () => {
namespace: '_namespace_',
hostname: 'app.gitfoo.com',
cookieDomain: '.gitfoo.com',
userId: null,
};
snowplowSpy = jest.spyOn(window, 'snowplow');
});
......@@ -34,6 +35,7 @@ describe('Tracking', () => {
contexts: { webPage: true },
formTracking: false,
linkClickTracking: false,
userId: null,
});
});
......@@ -41,15 +43,18 @@ describe('Tracking', () => {
initUserTracking();
expect(snowplowSpy).toHaveBeenCalledWith('enableActivityTracking', 30, 30);
expect(snowplowSpy).toHaveBeenCalledWith('trackPageView');
expect(snowplowSpy).not.toHaveBeenCalledWith('setUserId');
expect(snowplowSpy).not.toHaveBeenCalledWith('enableFormTracking');
expect(snowplowSpy).not.toHaveBeenCalledWith('enableLinkClickTracking');
window.snowplowOptions = Object.assign({}, window.snowplowOptions, {
formTracking: true,
linkClickTracking: true,
userId: '1',
});
initUserTracking();
expect(snowplowSpy).toHaveBeenCalledWith('setUserId', '1');
expect(snowplowSpy).toHaveBeenCalledWith('enableFormTracking');
expect(snowplowSpy).toHaveBeenCalledWith('enableLinkClickTracking');
});
......
......@@ -13,14 +13,17 @@ describe Gitlab::Tracking do
describe '.snowplow_options' do
it 'returns useful client options' do
expect(described_class.snowplow_options(nil)).to eq(
expected_fields = {
namespace: 'gl',
hostname: 'gitfoo.com',
cookieDomain: '.gitfoo.com',
appId: '_abc123_',
formTracking: true,
linkClickTracking: true
)
linkClickTracking: true,
userId: nil
}
expect(subject.snowplow_options(nil, nil)).to match(expected_fields)
end
it 'enables features using feature flags' do
......@@ -29,11 +32,12 @@ describe Gitlab::Tracking do
:additional_snowplow_tracking,
'_group_'
).and_return(false)
expect(described_class.snowplow_options('_group_')).to include(
addition_feature_fields = {
formTracking: false,
linkClickTracking: false
)
}
expect(subject.snowplow_options('_group_', nil)).to include(addition_feature_fields)
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