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

Extended experiments URL parser to handle originalHash #4536

Merged
merged 8 commits into from
Aug 24, 2016
Merged

Extended experiments URL parser to handle originalHash #4536

merged 8 commits into from
Aug 24, 2016

Conversation

tdrl
Copy link

@tdrl tdrl commented Aug 16, 2016

When the target page is served via the Google Search page, experiment parameters will be passed in the hash fragment. In turn, that is translated to win.location.originalHash by the AMP viewer. This change allows the A4A experiments configuration URL parser to find experiment control parameters in the originalHash or hash fields.

let queryString = win.location.search;
// If we're inside the AMP viewer, the hash fragment will have been moved
// to the originalHash field.
const hash = win.location.originalHash || win.location.hash;
Copy link
Contributor

Choose a reason for hiding this comment

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

We really need a hash service.

Copy link
Author

Choose a reason for hiding this comment

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

I don't disagree. Is this something that's near enough term that I should block this on? (Would rather not, since this PR is required for the full A4A experiment to get running.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Nah, let's go ahead with this for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

viewer.getFragment() is the preferred way to get hash.

Copy link
Author

Choose a reason for hiding this comment

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

Woo. Is it really necessary to go to a Promise just to get at the hash? This will be running in the code that decides whether to execute A4A or the existing 3p iframe amp-ad implementation. I'd prefer not to have to block for an unknown time before making this decision. Is there a (small, fast) synchronous way to do this?

Also, it looks like viewer.getFragment() only accesses location.hash, but when I look at AMP docs in the viewer through Chrome dev console, I only see stuff populated in location.originalHash. Looks like viewer.constructor() moves it from hash to originalHash? So will getFragment() even get at that?

But, really, what I want here is "the URL parameter exp, whether it's set in the host page URL search fragment or the hash/originalHash fragment". It looks like viewer.getParam() might give me that? Would that be a simpler way to go here?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes. If exp is the only thing you want, viewer.getParam('exp') is a better choice. Right now originalHash is only used to defer mode. Viewer param can be passed to runtime in multiple places, see this code.

Please see previous discussion

@tdrl
Copy link
Author

tdrl commented Aug 22, 2016

Migrated to using viewerFor(win).getParam to handle viewer / hash params. Thanks for the tips! PTAL.

it(`should find viewer param when pattern is ${hashBase}`, () => {
win.location.hash = hashBase.replace('PARAM', 'a4a:-1');
installViewerService(win);
viewerFor(win);
Copy link
Contributor

Choose a reason for hiding this comment

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

is this call necessary? It seems to be a simple getter with no side-effect.

Copy link
Author

Choose a reason for hiding this comment

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

Good catch! I admit that this sequence is a mystery to me. :-( This goes back to "couldn't figure out how to get Viewer to (re-)parse the hash parameters." This sequence was the result of mutational testing, and it was the one that worked. I stopped when it ran and passed tests, and didn't try all the ways I could strip it down. :-)

I dropped the viewerFor line.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's a problem that we didn't make it consistent.

for many other services, calling xxxFor(win) will install the service.

expect(googleAdsIsA4AEnabled(win, element, EXP_ID, EXTERNAL_BRANCHES,
INTERNAL_BRANCHES), 'googleAdsIsA4AEnabled').to.be.true;
expect(win.document.cookie).to.be.null;
expect(rand.called, 'rand called at least once').to.be.false;
Copy link
Contributor

Choose a reason for hiding this comment

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

try this: expect(rand).to.not.be.called

Copy link
Author

Choose a reason for hiding this comment

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

Oh, nifty! I didn't realize you could do that. Changed in multiple places. Thanks!

@lannka
Copy link
Contributor

lannka commented Aug 23, 2016

LGTM

@lannka lannka added LGTM and removed NEEDS REVIEW labels Aug 23, 2016
expect(googleAdsIsA4AEnabled(win, element, EXP_ID, EXTERNAL_BRANCHES,
INTERNAL_BRANCHES), 'googleAdsIsA4AEnabled').to.be.true;
expect(win.document.cookie).to.be.null;
expect(rand.called, 'rand called at least once').to.be.false;
Copy link
Contributor

Choose a reason for hiding this comment

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

one more left.

I think you can also remove the message, as it will say something similar for you.

Copy link
Author

Choose a reason for hiding this comment

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

D'oh! Ok, this time I did a global search for rand.called and for to.not.be.called. I think (hope?) I got them all. Also removed messages. PTAL.

@lannka lannka merged commit 7df92dd into ampproject:master Aug 24, 2016
@tdrl tdrl deleted the a4a-url-parse-update branch August 24, 2016 13:12
ariangibson pushed a commit to Mixpo/amphtml that referenced this pull request Sep 7, 2016
* Extended experiments URL parser to handle originalHash

* Intermediate state: Update in place, but working on tests.  Do not merge to master.

* Update method of hash parsing for URL params.

* Remove a debugging-only log message.

* Lint fixes.

* Removed unnecessary viewerFor call.

* Updated expectations for rand.called.

* Updated expectations for rand.called.
mityaha pushed a commit to ooyala/amphtml that referenced this pull request Nov 30, 2016
* Extended experiments URL parser to handle originalHash

* Intermediate state: Update in place, but working on tests.  Do not merge to master.

* Update method of hash parsing for URL params.

* Remove a debugging-only log message.

* Lint fixes.

* Removed unnecessary viewerFor call.

* Updated expectations for rand.called.

* Updated expectations for rand.called.
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.

3 participants