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
amp-ima-video: add support for IMASdkSettings as JSON. #10525
Conversation
@@ -3145,6 +3145,19 @@ tags: { | |||
} | |||
} | |||
} |
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.
This is a guess based on the above config - let me know if this needs to be different.
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.
@shawnbuso You'll also want the cdata
section as we don't allow html comments within script
tags.
@Gregable as fyi for validation 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.
Cool, thanks - updated.
ads/google/imaVideo.js
Outdated
@@ -330,6 +333,9 @@ export function imaVideo(global, data) { | |||
videoPlayer.appendChild(htmlToElement(child)); | |||
}); | |||
} | |||
if (data.imaSettings) { | |||
imaSettings = JSON.parse(data.imaSettings); |
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.
please use tryParseJson
(already imported)
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.
ads/google/imaVideo.js
Outdated
@@ -378,12 +384,37 @@ export function imaVideo(global, data) { | |||
false); | |||
}); | |||
|
|||
// Handle settings that need to be set before the AdDisplayContainer is | |||
// created. | |||
if (imaSettings.locale) { |
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.
please use imaSettings['locale']
format everywhere since tryParseJson returns a JsonObject
and we like to enable a Closure Compiler optimization which will obfuscate properties
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.
Good catch, thanks - done.
ads/google/imaVideo.js
Outdated
if (!skippedSettings.includes(setting)) { | ||
// Change e.g. 'ppid' to 'setPpid'. | ||
const methodName = | ||
'set' + setting.charAt(0).toUpperCase() + setting.slice(1); |
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.
use camelCaseToTitleCase
from style.js
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.
ads/google/imaVideo.js
Outdated
// Change e.g. 'ppid' to 'setPpid'. | ||
const methodName = | ||
'set' + setting.charAt(0).toUpperCase() + setting.slice(1); | ||
adsLoader.getSettings()[methodName](imaSettings[setting]); |
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.
add safety check to prevent exception if user-provided setting happens to be wrong.
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.
@@ -93,6 +94,13 @@ class AmpImaVideo extends AMP.BaseElement { | |||
this.element.setAttribute( | |||
'data-child-elements', JSON.stringify(children)); | |||
} | |||
|
|||
// Handle IMASetting JSON | |||
const scriptElement = this.element.getElementsByTagName('script')[0]; |
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.
childElementsByTag
from dom.js
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.
|
||
// Handle IMASetting JSON | ||
const scriptElement = this.element.getElementsByTagName('script')[0]; | ||
if (scriptElement.type == 'application/json') { |
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.
null check as <script>
is optional also use isJsonScriptTag
from dom.js
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.
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.
LGTM. Small nit and there is a lint error. good to merge when tests pass.
const scriptElement = childElementsByTag(this.element, 'SCRIPT')[0]; | ||
if (scriptElement && isJsonScriptTag(scriptElement)) { | ||
this.element.setAttribute( | ||
'data-ima-settings', scriptElement./*REVIEW*/innerHTML); |
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/
@Gregable I think something is wrong with my validator change - the validator build is failing (see travis logs). Any tips? I need it to be set up to allow |
validator/validator-main.protoascii
Outdated
spec_name: "amp-ima-video > script[type=application/json]" | ||
unique: true | ||
mandatory_parent: "AMP-IMA-VIDEO" | ||
attrs: { |
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's add a dispatch key attribute. This will look like:
attrs: {
name: "amp-ima-video-config"
value: ""
dispatch_key: NAME_DISPATCH
}
This will force the validator to only consider this tagspec for matching if that attribute is present on the tag it's matching. Therefore this tagspec won't produce errors for generic <script>
tags found in the document.
Users will need to add that attribute to their script tag, if that's OK.
In the future, we can improve the validator matching and relax this requirement.
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 the dispatch key be on type
? seems to work for amp-analytics
: https://github.com/ampproject/amphtml/blob/master/extensions/amp-analytics/validator-amp-analytics.protoascii#L35
Bump :) I think I have the approvals I need and comments should be resolved. |
@shawnbuso github is showing the branch has conflicts in amp-ima-video.js that need to be resolved. |
validator/validator-main.protoascii
Outdated
name: "amp-ima-video-settings" | ||
mandatory: true | ||
value: "" | ||
dispatch_key: NAME_DISPATCH |
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.
@Gregable @shawnbuso I think dispatch_key
can to go "type"
attr so we don't need the extra amp-ima-video-settings
attribute.
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.
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 looks like that broke things again - I changed it and re-pushed so we can check out the logs.
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.
What @aghassemi suggested would be a different value for dispatch_key
when on type
. It would be dispatch_key: NAME_VALUE_PARENT_DISPATCH
Whoops, my mistake - merge conflicts 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.
one request and can merge when tests pass. thanks!
@@ -65,6 +66,12 @@ VAST-compliant ad response (for examples, see | |||
<source src="foo.mp4" type="video/mp4"> | |||
<source src="foo.webm" type="video/webm"> | |||
<track label="English subtitles" kind="subtitles" srclang="en" src="subtitles.vtt"> | |||
<script type="application/json" amp-ima-video-settings> |
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.
remove amp-ima-video-settings
now that it is gone from validator
Hi guys, Here is my code: Thanks |
Hi @plammy , I just gave your code a try and worked for me (well, at least the locale:"de" part that I could see and verify). We just did a prod push yesterday, that may explain why I see a different result. Could you give it another try? Thanks, |
@aghassemi Yes thank you. Now it works fine for me :) |
@shawnbuso when this release will be available for prod use? Is there any way to use in prod? |
@omeryounus Adding |
thanks @aghassemi working fine... |
Fixes #10196
Adds support for providing IMASdkSettings as a
<script type="application/json">
child note ofamp-ima-video
.