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): utm params for all developer.google.com links #4972

Closed
wants to merge 12 commits into from

Conversation

kdzwinel
Copy link
Collaborator

@kdzwinel kdzwinel commented Apr 15, 2018

Closes #4663

Appends additional params to all developer.google.com links for easier tracking:

  • utm_source - hardcoded to 'lighthouse'
  • utm_medium - "cli", "ext" (extension), "cdt" (devtools), "nm" (node module) depending on how LH was run
  • utm_content - "pass", "fail" depending on audit result (if available)

Example:
screen shot 2018-07-26 at 17 38 45

Explanation

As discussed in April, channel is now part of the lighthouseVersion in the output object (e.g. '3.0.3+cli'). Applications can pass channel value via LH_CHANNEL environment variable.

The algorithm is as follows:

  • if LH is run via extension, LH_CHANNEL is hardcoded to 'ext'
  • if LH is run via Chrome DevTools, LH_CHANNEL is hardcoded to 'cdt'
  • if LH_CHANNEL environment variable is set, it's used
  • if LH_CHANNEL is not set:
    • and if LH is run via lighthouse-cli/index.js, LH_CHANNEL is set to 'cli'
    • and if LH is not run via lighthouse-cli/index.js, LH_CHANNEL is set to 'nm'


module.exports = {
name: 'CLI',
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was also considering having one const ENVIRONMENT = 'replace-me' in the runner.js instead of three environment/ files, but replacing imports instead of values seemed a tiny bit cleaner.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I agree abstractly, unfortunately it doesn't seem as easy as I expected in browserify 😆

is there any simpler way with less manual? I'm no browserify expert, maybe something similar to the webpack define plugin or simple shim replacement without custom transform?

with the audits/gatherers we manually do something fancy without a transform which is what makes me hopeful :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

webpack define plugin

that was a good tip, thanks! (after a lot of googling) I found similar solution for browserify 👌

@@ -10,7 +10,8 @@ The top-level Lighthouse Result object (LHR) is what the lighthouse node module

| Name | Description |
| - | - |
| lighthouseVersion | The version of Lighthouse with which this result were generated. |
| lighthouseVersion | The version of Lighthouse with which this result was generated. |
| lighthouseEnvironment | The environment in which Lighthouse result was generated (CLI, extension, DevTools). |
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be happy to change that name, I couldn't figure out anything better

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should figure out where this fits into the LHR reshuffling. Like the discussion on runtimeConfig, maybe this should move into the settings property (whatever we end up calling it) that's copied over from the config settings and has all the flags, throttling settings, etc, used in this run

@@ -110,6 +110,7 @@ class Runner {
// Entering: conclusion of the lighthouse result object
// @ts-ignore - Needs json require() support
const lighthouseVersion = /** @type {string} */ (require('../package.json').version);
const lighthouseEnvironment = /** @type {string} */ (require('./environment/cli').name);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'./environment/cli' this part gets replaced by browserify

'use strict';

module.exports = {
name: 'extension',
Copy link
Collaborator

@patrickhulce patrickhulce Apr 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since these seems to be display-oriented how about 'Extension' alternatively we make all these lowercase and just switch in the report to compute the display version

either seems ok with me

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, changing this to 'Extension' SGTM 👍

const environment = file.path.includes('-ext-') ? 'Extension' : 'DevTools';
let bundle = browserify(file.path, {
insertGlobalVars: {
'global.ENVIRONMENT': () => `"${environment}"`,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I won't be able to answer any questions starting with "why" regarding this line. It's just how it's done in browserify e.g. we can't just have () => environment here because we will end up with {"ENVIRONMENT": DevTools} (note missing quotes) in the result code 🤷‍♂️

Copy link
Collaborator

@patrickhulce patrickhulce Apr 17, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hurray much easier! :D 🎉

yeah webpack works similarly where it's basically straight up string replacement, FWIW I've usually seen JSON.stringify(environment) to try to get this message across but I don't have a strong opinion

@brendankenny @paulirish either way make more sense to you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to add another possibility, we could use https://github.com/hughsk/envify and then use process.env.LH_ENVIRONMENT or whatever. That would avoid the mystery global variable and allow overriding on the CLI side if for some reason you'd want to do that (WPT or something?)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sticking with insertGlobalVars is in my opinion a better ideas as it's native browserify. We can still use env vars with process.env which makes it a bit more verbose. I would try to use JSON.stringify as it's more common due webpacks implementation as patrick suggested.

@kdzwinel
Copy link
Collaborator Author

@patrickhulce I replaced custom browserify transform with something less manual + added tests for convertMarkdownLinkSnippets. PTAL

@@ -110,6 +110,8 @@ class Runner {
// Entering: conclusion of the lighthouse result object
// @ts-ignore - Needs json require() support
const lighthouseVersion = /** @type {string} */ (require('../package.json').version);
// @ts-ignore - ENVIRONMENT varialble is not declared
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brendankenny not sure how to best handle that. Should we have a separate ts file with interface Global?

Copy link
Member

@brendankenny brendankenny Apr 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're already stomping all over the global scope in

declare global {
// Augment global Error type to include node's optional `code` property
// see https://nodejs.org/api/errors.html#errors_error_code
interface Error {
code?: string;
}
:) Not sure if you can just declare it there or have to augment global (or Window, or however they do the global mapping for node) like Error is being augmented in those lines

@paulirish
Copy link
Member

paulirish commented Apr 25, 2018

cli, ext, cdt, nm, wpt, lr
LH_CHANNEL
3.0.0+cli

@rviscomi
Copy link
Member

What's the status of this PR?

// Fix an issue with imported speedline code that doesn't brfs well.
return bundle.transform('./fs-transform', {global: true})
// Replace LH_CHANNEL enviroment variable with a string
.transform('./lh-channel-transform', {channel, global: true})
Copy link
Collaborator Author

@kdzwinel kdzwinel Jul 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a custom find&replace transform. I couldn't use envify because it's based on esprima 4 (latest) which doesn't support object rest/spread :(

@kdzwinel
Copy link
Collaborator Author

@rviscomi just updated!

@patrickhulce @paulirish Sorry it took so long, but I updated this PR as discussed in April (see the description)

// Fix an issue with imported speedline code that doesn't brfs well.
return bundle.transform('./fs-transform', {global: true})
// Replace LH_CHANNEL enviroment variable with a string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: s/enviroment/environment/

Copy link
Member

@rviscomi rviscomi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I'll defer to @patrickhulce @brendankenny or @brendankenny if they have any technical feedback.

Copy link
Collaborator

@patrickhulce patrickhulce left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM thanks @kdzwinel!!

@@ -106,7 +106,12 @@ class Runner {
}

// Entering: conclusion of the lighthouse result object
const lighthouseVersion = require('../package.json').version;

// @ts-ignore - Needs json require() support
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brendankenny do you have any tips for him here?

not sure why this would be be necessary now when it wasn't before 🤔

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is just a bad merge or rebase. I just removed this ts-ignore in the last week or two

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call, I removed it 👍

if (metadata && metadata.channel) {
url.searchParams.set('utm_medium', metadata.channel);
}
if (metadata && metadata.rating) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh my, I'm really regretting us calling that pass/fail string rating now 😆

Copy link
Member

@brendankenny brendankenny left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few last things.

Can you also add a check to extension-test.js, so it gets a test through the full pipeline? It should be straightforward to grab a description or two and make sure the URL has the utm params and channel is set to ext

Thanks for sticking with this!!

lighthouse-core/report/html/renderer/dom.js Outdated Show resolved Hide resolved
lighthouse-extension/lh-channel-transform.js Outdated Show resolved Hide resolved
lighthouse-extension/lh-channel-transform.js Outdated Show resolved Hide resolved
@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored or co-authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this state. It's up to you to confirm consent of all the commit author(s), set the cla label to yes (if enabled on your project), and then merge this pull request when appropriate.

@mattzeunert
Copy link
Contributor

@brendankenny Hey, I'm taking over this PR from Konrad. I've addressed your comments, can you take a look?

@brendankenny
Copy link
Member

@mattzeunert Sorry for being slow. Also sent you a message out of band, but I think we're going to move this to chillin and wait on it for a little while.

There's been a lot of churn in the bundle/channel entry points, file names, and control flow, so things have changed a good bit in this area since the PR started. The changes needed for utm params could probably get simpler (e.g. maybe no browserify transform) but also needs to handle new stuff (like the lr channel). Rather than fixing this PR and then changing it a few times while things continue to churn before CDS, let's just put it off for a little while.

@brendankenny
Copy link
Member

Let's close instead of chilling indefinitely. I'll leave a note on #4663

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants