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