I2I: amp-ad-exit indirect target
Summary
Enable stateful exit behavior in <amp-ad-exit>, by adding variable exit target that can be changed at runtime.
Motivation
Many HTML5 ads display multiple products using a swipeable image gallery. These ads usually have a header/footer area or a click-to-action button that exits, when clicked, to the URL of currently displayed product. This behavior is hard to reproduce in AMPHTML using <amp-carousel> and <amp-ad-exit>, as the <amp-ad-exit> does not maintain a state that responds to user interaction, e.g. a changed active product item index.
Details
In addition to the exit targets that we currently have in the JSON configuration (which are referenced as fixed exit targets hereafter), <amp-ad-exit> will maintain a separate mapping of variable targets to fixed targets, which do not define exit URLs by themselves, but instead point to one of the fixed targets.
A new action will be added to <amp-ad-exit>:
setVariable(name=STRING, target=STRING)
| Name | Value | Meaning |
|---|---|---|
| name | string | name of the variable target |
| target | string | name of the target that this variable target points to |
When this action is invoked, target will be checked to ensure it's the name of one of the existing fixed targets, and the specified variable target will point to the new value.
To provide default value for an variable target, we have following options:
Option 1
In the JSON configuration, add a new property pointTo to NavigationTargetConfig. Whenever pointTo is provided, the target is treated as variable and all other fields are ignored.
{
"targets": {
"Product_0_url": {
"final_url": "https://example.com/product0"
},
"current": {
"pointTo": "Product_0_url"
}
}
}
Variable target is still exited through the exit action. The implementation of exit action will be changed to look up both variable targets and fixed targets.
Option 2
Allow the exit action to exit through the variable target, and provide a default value through argument, e.g.
exit(variable="current", default="Product_0_url" [, optional URL params])
This option does not require defining variable targets in JSON configuration, and arbitrary variable target name can be added through setVariable action.
Example
<amp-ad-exit id="exit-api">
<script type="application/json">
{
"targets": {
"Product_0_url": {
"final_url": "https://example.com/product0"
},
"Product_1_url": {
"final_url": "https://example.com/product1"
},
"Product_2_url": {
"final_url": "https://example.com/product2"
}
}
}
</script>
</amp-ad-exit>
<div id="header"
on="tap:exit-api.exit(variable='current', default='Product_0_url')">
</div>
<amp-carousel type="slides" autoplay
on="slideChange:exitSelector.toggle(index=event.index, value=true)">
<div on="tap:exit-api.exit(target='Product_0_url')">Product 0</div>
<div on="tap:exit-api.exit(target='Product_1_url')">Product 1</div>
<div on="tap:exit-api.exit(target='Product_2_url')">Product 2</div>
</amp-carousel>
<amp-selector id="exitSelector"
on="select:exit-api.setVariable(name='current', target=event.targetOption)>
<option option="Product_0_url"></option>
<option option="Product_1_url"></option>
<option option="Product_2_url"></option>
</amp-selector>
<button id="CTA"
on="tap:exit-api.exit(variable='current', default='Product_0_url')">
Buy now
</button>
<div id="footer"
on="tap:exit-api.exit(variable='current', default='Product_0_url')">
</div>
/cc @ampproject/wg-approvers @clawr @lannka @calebcordry
Hi Hongfei, Caleb, do you have any comments, especially on defining default value for an indirect target? We slightly prefer the second option.
I think I also prefer the API for option 2, but I am not super familiar with the actions code, @choumx I think is the expert. Will, do you have a preference here?
I will try to avoid introducing a new "glue" (indirectTarget), and use existing ones such as amp_state. or, we should introduce specific action for amp-selector that can pass the selected option out.
@choumx knows better in this space.
We have considered using AMP state, e.g.
<button on="tap:exit-api.exit(target=currentExitTarget)">CTA</button>
where currentExitTarget is an AMP state variable. But actions do not support passing AMP state variable value as argument.
Or add a bindable attribute to <amp-ad-exit>:
<amp-ad-exit [currentTarget]="currentExitTarget">...</amp-ad-exit>
and an action that reads this attribute and exits to the bound target name exitCurrent().
This approach is limited to our single use case, i.e. exit to the URL of currently displayed product.
And another drawback of using AMP state is, low trust event is not allowed to modify amp state, so it's impossible for an autoplaying amp-carousel to change the current exit target when slide changes:
<amp-carousel autoplay
on="slideChange:AMP.setState({currentExitTarget: 'Product_' + event.index + '_url'})> ... </amp-carousel>
or, we should introduce specific action for amp-selector that can pass the selected option out.
For this option, do you mean something like:
<button on="tap:exit-api.exit(target=exitSelector.getTargetOption())">CTA</button>
This would be nice if we can use a return value as an argument.
@choumx Hi William, could you help take a look? We want to have this feature soon for building dynamic AMPHTML ads. Thanks!
@lannka Hi Hongfei, it seems William is on vacation. Do you have other comments? Does the proposal look good to you? In the meantime, I've created a PR that implemented option #2.
@jridgewell to take a look
Sorry, I don't really have context on what any of this means. A few questions to get me up to speed:
- What does amp-ad-exit actually do? I read the documentation and my eyes glaze over.
- What is an exit target?
- What does making one "indirect" mean? How is this different than what is currently possible?
- What needs to change in runtime to support this?
Hi Justin, thanks for taking a look!
- amp-ad-exit is an element that handles all exit events in an ad, where
exitmeans redirecting to advertiser's page after user clicks on something. It provides many features like fast-click protection, border protection to eliminate unintentional clicks. - in the simplest case, an exit target gives a name to a URL, e.g.
product_0:"http://test.com/product0", which can be referenced later in an exit action,exit(target="product_0") - indirect target is introduced to allow amp-ad-exit to maintain some state. An indirect target does not map to a URL directly, but instead points to another target defined in the configuration. In the example under the proposal, I used an indirect target called
currentto point to the URL of currently displayed product. - we only need to add code in amp-ad-exit to support this new behavior https://github.com/ampproject/amphtml/pull/26517/files#diff-63cf316e6647dc9b56b2d7969d48f528
Thanks! I don't think this requires any runtime changes, so @lannka's approval is enough. We do need to review the PR, but I think it'll be fine.
Thanks Justin!
@lannka Hi Hongfei, as I mentioned in the previous comment, amp-state is not a good option for this feature, also adding an action to amp-selector to return a value would require a big change to the AMP runtime, it seems that right now no action can have a return value.
SG
Sorry for the late reply. I considered some changes we could make to amp-bind to support this but overall the proposed solution SGTM.
Some drive-by bikeshed: consider using "variable" instead of "indirect" and keeping the same "exit" action for a more readable API.
<amp-selector id="exitSelector"
on="select:exit-api.setVariable(name='current', target=event.targetOption)>
...
</amp-selector>
<button id="CTA"
on="tap:exit-api.exit(variable='current', default='Product_0_url')">
Buy now
</button>
Thanks William! I updated the proposal following your suggestion.
I will update the doc when the change is released.