Commit 0967d5c0 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '216199-track-wiki-page-views' into 'master'

Track wiki page views in Snowplow

See merge request gitlab-org/gitlab!35784
parents b541af36 3e1310e3
import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils'; import { GlBreakpointInstance as bp } from '@gitlab/ui/dist/utils';
import { s__, sprintf } from '~/locale'; import { s__, sprintf } from '~/locale';
import Tracking from '~/tracking';
const MARKDOWN_LINK_TEXT = { const MARKDOWN_LINK_TEXT = {
markdown: '[Link Title](page-slug)', markdown: '[Link Title](page-slug)',
...@@ -8,6 +9,9 @@ const MARKDOWN_LINK_TEXT = { ...@@ -8,6 +9,9 @@ const MARKDOWN_LINK_TEXT = {
org: '[[page-slug]]', org: '[[page-slug]]',
}; };
const TRACKING_EVENT_NAME = 'view_wiki_page';
const TRACKING_CONTEXT_SCHEMA = 'iglu:com.gitlab/wiki_page_context/jsonschema/1-0-0';
export default class Wikis { export default class Wikis {
constructor() { constructor() {
this.sidebarEl = document.querySelector('.js-wiki-sidebar'); this.sidebarEl = document.querySelector('.js-wiki-sidebar');
...@@ -57,6 +61,8 @@ export default class Wikis { ...@@ -57,6 +61,8 @@ export default class Wikis {
window.onbeforeunload = null; window.onbeforeunload = null;
}); });
} }
Wikis.trackPageView();
} }
handleWikiTitleChange(e) { handleWikiTitleChange(e) {
...@@ -97,4 +103,17 @@ export default class Wikis { ...@@ -97,4 +103,17 @@ export default class Wikis {
classList.remove('right-sidebar-expanded'); classList.remove('right-sidebar-expanded');
} }
} }
static trackPageView() {
const wikiPageContent = document.querySelector('.js-wiki-page-content[data-tracking-context]');
if (!wikiPageContent) return;
Tracking.event(document.body.dataset.page, TRACKING_EVENT_NAME, {
label: TRACKING_EVENT_NAME,
context: {
schema: TRACKING_CONTEXT_SCHEMA,
data: JSON.parse(wikiPageContent.dataset.trackingContext),
},
});
}
} }
...@@ -128,10 +128,6 @@ label { ...@@ -128,10 +128,6 @@ label {
display: inline; display: inline;
} }
.wiki-content {
margin-top: 35px;
}
.form-control::placeholder { .form-control::placeholder {
color: $gl-text-color-tertiary; color: $gl-text-color-tertiary;
} }
......
...@@ -128,4 +128,13 @@ module WikiHelper ...@@ -128,4 +128,13 @@ module WikiHelper
raise NotImplementedError, "Unknown wiki container type #{wiki.container.class.name}" raise NotImplementedError, "Unknown wiki container type #{wiki.container.class.name}"
end end
end end
def wiki_page_tracking_context(page)
{
'wiki-format' => page.format,
'wiki-title-size' => page.title.bytesize,
'wiki-content-size' => page.raw_content.bytesize,
'wiki-directory-nest-level' => page.path.scan('/').count
}
end
end end
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
= (s_("WikiHistoricalPage|You can view the %{most_recent_link} or browse the %{history_link}.") % { most_recent_link: most_recent_link, history_link: history_link }).html_safe = (s_("WikiHistoricalPage|You can view the %{most_recent_link} or browse the %{history_link}.") % { most_recent_link: most_recent_link, history_link: history_link }).html_safe
.gl-mt-3.gl-mb-3 .gl-mt-3.gl-mb-3
.md{ data: { qa_selector: 'wiki_page_content' } } .js-wiki-page-content.md{ data: { qa_selector: 'wiki_page_content', tracking_context: wiki_page_tracking_context(@page).to_json } }
= render_wiki_content(@page) = render_wiki_content(@page)
= render 'shared/wikis/sidebar' = render 'shared/wikis/sidebar'
---
title: Track wiki page views in Snowplow
merge_request: 35784
author:
type: changed
import { escape } from 'lodash';
import Wikis from '~/pages/shared/wikis/wikis'; import Wikis from '~/pages/shared/wikis/wikis';
import Tracking from '~/tracking';
import { setHTMLFixture } from './helpers/fixtures'; import { setHTMLFixture } from './helpers/fixtures';
describe('Wikis', () => { describe('Wikis', () => {
...@@ -122,4 +124,32 @@ describe('Wikis', () => { ...@@ -122,4 +124,32 @@ describe('Wikis', () => {
}); });
}); });
}); });
describe('trackPageView', () => {
const trackingPage = 'projects:wikis:show';
const trackingContext = { foo: 'bar' };
const showPageHtmlFixture = `
<div class="js-wiki-page-content" data-tracking-context="${escape(
JSON.stringify(trackingContext),
)}"></div>
`;
beforeEach(() => {
setHTMLFixture(showPageHtmlFixture);
document.body.dataset.page = trackingPage;
jest.spyOn(Tracking, 'event').mockImplementation();
Wikis.trackPageView();
});
it('sends the tracking event and context', () => {
expect(Tracking.event).toHaveBeenCalledWith(trackingPage, 'view_wiki_page', {
label: 'view_wiki_page',
context: {
schema: 'iglu:com.gitlab/wiki_page_context/jsonschema/1-0-0',
data: trackingContext,
},
});
});
});
}); });
...@@ -104,4 +104,24 @@ RSpec.describe WikiHelper do ...@@ -104,4 +104,24 @@ RSpec.describe WikiHelper do
expect(helper.wiki_sort_title('unknown')).to eq('Title') expect(helper.wiki_sort_title('unknown')).to eq('Title')
end end
end end
describe '#wiki_page_tracking_context' do
let_it_be(:page) { create(:wiki_page, title: 'path/to/page 💩', content: '💩', format: :markdown) }
subject { helper.wiki_page_tracking_context(page) }
it 'returns the tracking context' do
expect(subject).to eq(
'wiki-format' => :markdown,
'wiki-title-size' => 9,
'wiki-content-size' => 4,
'wiki-directory-nest-level' => 2
)
end
it 'returns a nest level of zero for toplevel files' do
expect(page).to receive(:path).and_return('page')
expect(subject).to include('wiki-directory-nest-level' => 0)
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