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

amp-ima-video: add support for IMASdkSettings as JSON. #10525

Merged
merged 12 commits into from Aug 2, 2017
Merged

amp-ima-video: add support for IMASdkSettings as JSON. #10525

merged 12 commits into from Aug 2, 2017

Conversation

shawnbuso
Copy link
Contributor

Fixes #10196

Adds support for providing IMASdkSettings as a <script type="application/json"> child note of amp-ima-video.

@@ -3145,6 +3145,19 @@ tags: {
}
}
}
Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool, thanks - updated.

@@ -330,6 +333,9 @@ export function imaVideo(global, data) {
videoPlayer.appendChild(htmlToElement(child));
});
}
if (data.imaSettings) {
imaSettings = JSON.parse(data.imaSettings);
Copy link
Contributor

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)

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.

@@ -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) {
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch, thanks - done.

if (!skippedSettings.includes(setting)) {
// Change e.g. 'ppid' to 'setPpid'.
const methodName =
'set' + setting.charAt(0).toUpperCase() + setting.slice(1);
Copy link
Contributor

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

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.

// Change e.g. 'ppid' to 'setPpid'.
const methodName =
'set' + setting.charAt(0).toUpperCase() + setting.slice(1);
adsLoader.getSettings()[methodName](imaSettings[setting]);
Copy link
Contributor

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.

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.

@@ -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];
Copy link
Contributor

Choose a reason for hiding this comment

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

childElementsByTag from dom.js

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.


// Handle IMASetting JSON
const scriptElement = this.element.getElementsByTagName('script')[0];
if (scriptElement.type == 'application/json') {
Copy link
Contributor

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

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.

Copy link
Contributor

@aghassemi aghassemi left a 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);
Copy link
Contributor

Choose a reason for hiding this comment

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

/OK/

@shawnbuso
Copy link
Contributor Author

shawnbuso commented Jul 20, 2017

@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 <script type="application/json">...</script> as a child of my tag.

spec_name: "amp-ima-video > script[type=application/json]"
unique: true
mandatory_parent: "AMP-IMA-VIDEO"
attrs: {
Copy link
Member

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

@shawnbuso
Copy link
Contributor Author

Bump :) I think I have the approvals I need and comments should be resolved.

@honeybadgerdontcare
Copy link
Contributor

@shawnbuso github is showing the branch has conflicts in amp-ima-video.js that need to be resolved.

name: "amp-ima-video-settings"
mandatory: true
value: ""
dispatch_key: NAME_DISPATCH
Copy link
Contributor

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

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 looks like that broke things again - I changed it and re-pushed so we can check out the logs.

Copy link
Contributor

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

@shawnbuso
Copy link
Contributor Author

Whoops, my mistake - merge conflicts resolved.

Copy link
Contributor

@aghassemi aghassemi left a 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>
Copy link
Contributor

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

@aghassemi aghassemi merged commit 3455879 into ampproject:master Aug 2, 2017
@shawnbuso shawnbuso deleted the settings branch August 4, 2017 14:16
@plammy
Copy link

plammy commented Aug 15, 2017

Hi guys,
who can I test this? I have dev-channel and amp-ima-video experimental features enabled. I have try to set the location to Germany and encrease the number of redirects to 8 but I don't see any changes.

Here is my code:
<amp-ima-video id="myVideo" width="640" height="360" data-tag="https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/ad_rule_samples&ciu_szs=300x250&ad_rule=1&impl=s&gdfp_req=1&env=vp&output=vmap&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ar%3Dpremidpost&cmsid=496&vid=short_onecue&correlator=" data-poster="https://ampbyexample.com/img/ima-poster.jpg"> <script type="application/json"> { "locale": "de", "numRedirects": 8 } </script> <source src="https://s0.2mdn.net/4253510/google_ddm_animation_480P.mp4" type="video/mp4"> </amp-ima-video>

Thanks

@aghassemi
Copy link
Contributor

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,
Ali

@plammy
Copy link

plammy commented Aug 18, 2017

@aghassemi Yes thank you. Now it works fine for me :)

@omeryounus
Copy link
Contributor

@shawnbuso when this release will be available for prod use? Is there any way to use in prod?

@aghassemi
Copy link
Contributor

@omeryounus Adding <meta name="amp-experiments-opt-in" content="amp-ima-video"> to your page will enable the experiment for that page, but please note that component is still considered experimental and therefore no guarantee on backward compability in regards to changes until it is officially launched.

@omeryounus
Copy link
Contributor

thanks @aghassemi working fine...

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.

None yet

9 participants