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
Conversation
// Note: this must happen before this.autoFormat_ is populated | ||
// since it may change the outcome. | ||
if (this.isInAdSizeOptimizationExperimentBranch()) { | ||
upgradePromise = this.maybeUpgradeToResponsive(); |
There was a problem hiding this comment.
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(() => {...```
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.'); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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']; |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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 => { |
There was a problem hiding this comment.
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(() => {
There was a problem hiding this comment.
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()) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 = ( |
There was a problem hiding this comment.
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;
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return aas-${this.getAdClientId()}
;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
d07ddc7
to
772658e
Compare
/** | ||
* Testing callback invoked when auto ad size settings are persisted in | ||
* localstorage. | ||
* {function()} */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing @Private
There was a problem hiding this comment.
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 ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the paren?
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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
9f4dec0
to
76789a5
Compare
extensions/amp-ad-network-adsense-impl/0.1/amp-ad-network-adsense-impl.js
Outdated
Show resolved
Hide resolved
extensions/amp-ad-network-adsense-impl/0.1/amp-ad-network-adsense-impl.js
Outdated
Show resolved
Hide resolved
* Actually upgrades the ad unit to responsive. | ||
*/ | ||
upgradeToResponsive() { | ||
this.element.setAttribute('height', ADSENSE_RSPV_WHITELISTED_HEIGHT); |
There was a problem hiding this comment.
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.'); |
There was a problem hiding this comment.
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*() { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
extensions/amp-ad-network-adsense-impl/0.1/test/test-amp-ad-network-adsense-impl.js
Outdated
Show resolved
Hide resolved
5e85f9c
to
099ce8a
Compare
099ce8a
to
447deb5
Compare
Merged the changes together with the previously landed refactoring PR PTAL. |
extensions/amp-ad-network-adsense-impl/0.1/test/test-responsive-state.js
Outdated
Show resolved
Hide resolved
There was a problem hiding this 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.
for the OWNERS bot check, @cramforce to approve the amp-localstorage.md change |
See #23568.