Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report(links): include utm params in links to docs #7441

Merged
merged 9 commits into from
Mar 20, 2019
Merged
20 changes: 19 additions & 1 deletion lighthouse-core/report/html/renderer/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ class DOM {
constructor(document) {
/** @type {Document} */
this._document = document;
/** @type {string} */
this._lighthouseChannel = 'unknown';
}

/**
Expand Down Expand Up @@ -125,11 +127,19 @@ class DOM {

// Append link if there are any.
if (linkText && linkHref) {
const url = new URL(linkHref);

const DEVELOPERS_GOOGLE_ORIGIN = 'https://developers.google.com';
if (url.origin === DEVELOPERS_GOOGLE_ORIGIN) {
url.searchParams.set('utm_source', 'lighthouse');
url.searchParams.set('utm_medium', this._lighthouseChannel);
mattzeunert marked this conversation as resolved.
Show resolved Hide resolved
}

const a = this.createElement('a');
a.rel = 'noopener';
a.target = '_blank';
a.textContent = linkText;
a.href = (new URL(linkHref)).href;
a.href = url.href;
element.appendChild(a);
}
}
Expand Down Expand Up @@ -159,6 +169,14 @@ class DOM {
return element;
}

/**
* The channel to use for UTM data when rendering links to the documentation.
* @param {string} lighthouseChannel
*/
setLighthouseChannel(lighthouseChannel) {
this._lighthouseChannel = lighthouseChannel;
}

/**
* @return {Document}
*/
Expand Down
2 changes: 2 additions & 0 deletions lighthouse-core/report/html/renderer/report-renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class ReportRenderer {
// Mutate the UIStrings if necessary (while saving originals)
const originalUIStrings = JSON.parse(JSON.stringify(Util.UIStrings));

this._dom.setLighthouseChannel(result.configSettings.channel || 'unknown');

const report = Util.prepareReportResult(result);

container.textContent = ''; // Remove previous report.
Expand Down
15 changes: 15 additions & 0 deletions lighthouse-core/test/report/html/renderer/dom-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe('DOM', () => {
global.URL = URL;
const {document} = new jsdom.JSDOM(TEMPLATE_FILE).window;
dom = new DOM(document);
dom.setLighthouseChannel('someChannel');
});

afterAll(() => {
Expand Down Expand Up @@ -115,6 +116,20 @@ describe('DOM', () => {
assert.equal(result.innerHTML, 'Ensuring `<td>` cells using the `[headers]` are ' +
'good. <a rel="noopener" target="_blank" href="https://dequeuniversity.com/rules/axe/3.1/td-headers-attr">Learn more</a>.');
});

it('appends utm params to the URLs with https://developers.google.com origin', () => {
const text = '[Learn more](https://developers.google.com/web/tools/lighthouse/audits/description).';

const result = dom.convertMarkdownLinkSnippets(text);
assert.equal(result.innerHTML, '<a rel="noopener" target="_blank" href="https://developers.google.com/web/tools/lighthouse/audits/description?utm_source=lighthouse&amp;utm_medium=someChannel">Learn more</a>.');
});

it('doesn\'t append utm params to non https://developers.google.com origins', () => {
const text = '[Learn more](https://example.com/info).';

const result = dom.convertMarkdownLinkSnippets(text);
assert.equal(result.innerHTML, '<a rel="noopener" target="_blank" href="https://example.com/info">Learn more</a>.');
});
});

describe('convertMarkdownCodeSnippets', () => {
Expand Down
19 changes: 19 additions & 0 deletions lighthouse-core/test/report/html/renderer/report-renderer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,25 @@ describe('ReportRenderer', () => {
assert.ok(output.querySelector('.score100'), 'has fireworks treatment');
});

it('should add LHR channel to doc link parameters', () => {
const lhrChannel = sampleResults.configSettings.channel;
// Make sure we have a channel in the LHR.
assert.ok(lhrChannel.length > 2);

const container = renderer._dom._document.body;
const output = renderer.renderReport(sampleResults, container);

const utmChannels = [...output.querySelectorAll('a[href*="utm_source=lighthouse"')]
.map(a => new URL(a.href))
.filter(url => url.origin === 'https://developers.google.com')
.map(url => url.searchParams.get('utm_medium'));

assert.ok(utmChannels.length > 20);
utmChannels.forEach(anchorChannel => {
assert.strictEqual(anchorChannel, lhrChannel);
});
});

it('renders `not_applicable` audits as `notApplicable`', () => {
const clonedSampleResult = JSON.parse(JSON.stringify(sampleResultsOrig));

Expand Down