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

✨ Implement ad size optimization for AdSense. #24375

Merged
merged 1 commit into from Oct 1, 2019

Conversation

riklund
Copy link
Contributor

@riklund riklund commented Sep 5, 2019

See #23568.

@riklund
Copy link
Contributor Author

riklund commented Sep 5, 2019

// Note: this must happen before this.autoFormat_ is populated
// since it may change the outcome.
if (this.isInAdSizeOptimizationExperimentBranch()) {
upgradePromise = this.maybeUpgradeToResponsive();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you move the isInAdSizeOptimizationExperimentBranch check into maybeUpgradeToResponsive saving having to create the upgradePromise variable? Given the function name implies it may do nothing, it seems like a cleaner divide:

'''return this.maybeUpgradeToResponsive().then(() => {...```

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, done.
I think I wanted the code to be obvious in terms of not changing behavior when the experiment wasn't active, but both ways work.

* Actually upgrades the ad unit to responsive.
*/
upgradeToResponsive() {
this.element.setAttribute('height', ADSENSE_RSPV_WHITELISTED_HEIGHT);
Copy link
Contributor

Choose a reason for hiding this comment

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

Might this cause reflow? Is it ok to change directly or do you need to use attemptSizeChange?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm a bit unsure. It seems at some point styles are set with css on the element which means these attributes don't actually matter for layout any more, but not entirely sure if that has happened yet in buildCallback?

We do need to change the values in the attributes, because otherwise the element validation (as it currently works) will fail (isValidElement will throw later on).

Copy link
Contributor

Choose a reason for hiding this comment

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

OK please add @lannka for review

Copy link
Contributor

Choose a reason for hiding this comment

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

AMP layout should have happened before this. But could you pls double confirm locally? (for example print logs that measures the height before / after this call.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done! Added console.error at the top/bottom of method printing
this.win.getComputedStyle(this.element, null).height
and it's the same value for both calls.

this.element.setAttribute('data-full-width', '');
this.element.setAttribute('data-auto-format', 'rspv');

devAssert(this.isValidElement(), 'Upgraded element is not valid.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Why would this assert fail? What about the change could make it invalid?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For example if we didn't set height/width to the exact values we do above.

Copy link
Contributor

Choose a reason for hiding this comment

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

devAssert will be stripped out from production binaries. is that what you want?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. This is intended to prevent accidentally breaking the feature during future development.

if (data['adClient'] != this.getAdClientId()) {
return;
}
const autoAdSizeStatus = !!data['enableAutoAdSize'];
Copy link
Contributor

Choose a reason for hiding this comment

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

if you truly expect it to the boolean true then better to do: const autoAdSizeStatus = data['enableAutoAdSize'] === true;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looking at some ad response, it seems it's actually '1' - updated.

this.win.removeEventListener('message', listener);
Services.storageForDoc(this.element)
.then(storage => {
return storage
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: you don't nee the braces and return: then(storage => storage.set...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

.then(storage => {
return storage
.set(this.getAdSizeOptimizationStorageKey(), autoAdSizeStatus)
.then(unused => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of unused just pass empty parens: then(() => {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@@ -629,6 +785,9 @@ export class AmpAdNetworkAdsenseImpl extends AmpA4A {

/** @override */
letCreativeTriggerRenderStart() {
if (this.isInAdSizeOptimizationExperimentBranch()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add this in onCreativeRender? It's a more explicit location of when creative rendering occurs as this function is more around analytics triggers and controlling if render should start.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(done)

* @return {string} ad client ID for the current ad unit.
*/
getAdClientId() {
const adClientId = (
Copy link
Contributor

Choose a reason for hiding this comment

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

let adClientId = (this.element.getAttribute('data-ad-client') || '').toLowerCase();
if (!/^ca-/i.test(adClientId)) adClientId = ca-${adClientId};
return adClientId;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

* @return {string} ad size optimization storage key.
*/
getAdSizeOptimizationStorageKey() {
return 'aas-' + this.getAdClientId();
Copy link
Contributor

Choose a reason for hiding this comment

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

return aas-${this.getAdClientId()};

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

/**
* Testing callback invoked when auto ad size settings are persisted in
* localstorage.
* {function()} */
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing @Private

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done.

// preceding logic (specifically responsive logic).
this.divertExperiments();
this.maybeAddSinglePassExperiment();
return (
Copy link
Contributor

Choose a reason for hiding this comment

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

Why the paren?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It gets automatically added by the linter:
gulp lint --local_changes --fix
Not sure if there's a way around that.

* Actually upgrades the ad unit to responsive.
*/
upgradeToResponsive() {
this.element.setAttribute('height', ADSENSE_RSPV_WHITELISTED_HEIGHT);
Copy link
Contributor

Choose a reason for hiding this comment

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

OK please add @lannka for review

@riklund riklund force-pushed the formatopia-1 branch 3 times, most recently from 9f4dec0 to 76789a5 Compare September 6, 2019 17:28
* Actually upgrades the ad unit to responsive.
*/
upgradeToResponsive() {
this.element.setAttribute('height', ADSENSE_RSPV_WHITELISTED_HEIGHT);
Copy link
Contributor

Choose a reason for hiding this comment

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

AMP layout should have happened before this. But could you pls double confirm locally? (for example print logs that measures the height before / after this call.

this.element.setAttribute('data-full-width', '');
this.element.setAttribute('data-auto-format', 'rspv');

devAssert(this.isValidElement(), 'Upgraded element is not valid.');
Copy link
Contributor

Choose a reason for hiding this comment

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

devAssert will be stripped out from production binaries. is that what you want?

@@ -422,6 +436,49 @@ describes.realWin(
);
expect(impl.iframe.id).to.equal('google_ads_iframe_3');
});

it('should write auto ad size data to localstorage', function*() {
Copy link
Contributor

Choose a reason for hiding this comment

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

new test should use async + await

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@riklund
Copy link
Contributor Author

riklund commented Sep 30, 2019

Merged the changes together with the previously landed refactoring PR

PTAL.

Copy link
Contributor

@rsimha rsimha left a comment

Choose a reason for hiding this comment

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

Changes to build-system/tasks/presubmit-checks.js LGTM.

@lannka lannka requested a review from cramforce October 1, 2019 19:00
@lannka
Copy link
Contributor

lannka commented Oct 1, 2019

for the OWNERS bot check, @cramforce to approve the amp-localstorage.md change

@lannka lannka merged commit 79878af into ampproject:master Oct 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants