amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Intent to Implement: Anchor Destination Expand on Click

Open keithwrightbos opened this issue 10 years ago • 9 comments

Description

Allow for expansion of anchor destination at the time of click to support adding click event information to the click string. The click x and y position are routinely sent with ad clicks. Bundling view information from an iframe subscribing to the intersectionObserver API will also be useful. Additionally allow for capturing post message based information to be included.

Implementation

Add 3 new macro expansion keys to URL Replacement Service (url-replacement.js):

  1. CLICK_X extracted from click event clientX property.
  2. CLICK_Y extracted from click event clientY property.
  3. POSTMESSAGE which uses value from most recent post message matching signature. Required input parameters: origin {string}: matched against origin of received post messages to valid sender key {string}: included in postmessage to link value and as additional validity check

Example postmessage (requires POSTMESSAGE_URL_REPLACE property whose value is not important): {POSTMESSAGE_URL_REPLACE: 1, key: ‘<key>’, value: ‘<value’>}

Replacement service will then be modified as follows:

  1. Attach click listener on document such that upon click event if target is an href, expands the href attribute
  2. Attach post message listener on window where each received message that includes POSTMESSAGE_URL_REPLACE, key, and value properties and stores value in object with property origin+key. Issue: need to guard against object growth due to large number of post messages with differing origin+key values.
  3. On click, determine if target is anchor with href attribute For CLICK_X, CLICK_Y macros, use the click event clientX/Y. It appears from here that click event may correctly provide the offset from the shadow root. Will verify this behavior. If not the case, will determine if element is within shadow root and if so, use it as the offset location.
  4. Use URL Replacement Service to expand keys specified in whitelist parameter (or all if not provided) with additional support for CLICK_X and CLICK_Y keys based on click event client X/Y values. For POSTMESSAGE macro, build origin+key from inputs and extract value from object storing most recent, matching postmessage value. If no valid post message was received, set empty string as value.
  5. Update href attribute, which will be used by browser as destination.

Example

<a href=”https://some.clickserver.com?foo=bar&nx=CLICK_X&ny=CLICK_Y&xm=POSTMESSAGE(foo.com,abcdef)&ym=POSTMESSAGE(foo.com,12345)&zm=POSTMESSAGE(evil.com,xss)&r=RANDOM”>click me</a>

Post Messages received from other_frame window to parent with ”foo.com” origin in the following order:

{POSTMESSAGE_URL_REPLACE: 1, key: abcdef, hello: ‘world’}
{key: 12345, hello: ‘world’}
{POSTMESSAGE_URL_REPLACE: 1, key: abcdef, hello: ‘newworld’}

Click on link with coordinates 123/456 results in destination (RANDOM macro replaced with random generated value of 91238): https://some.clickserver.com?foo=bar&nx=123&ny=456&xm=newworld&ym=&zm=&r=91238

Considerations

Ensure CLICK_X/CLICK_Y works appropriately in both shadow DOM and cross iframe cases. For shadow DOM, query selector will reference parent element for the creative via the following (where body element in the creative included unique id): # :parent

Alternative Implementations

Create a new amp-anchor extension with the following attributes. Disregarded due to need for retagging creatives however should complexity/use cases increase, this should be revisited.

  1. href: with expansion keys included (required)
  2. data-postmessage-origin: Type string. If given, stores last post message from origin given, to be used for POSTMESSAGE expansion key (optional).
  3. data-postmessage-source: Type element or string (used for query selector to element) of iframe from which the post message is sent. Used in combination with origin to determine source of post messages to be stored (optional)
  4. data-whitelist-keys: comma-separated list of expand macro keys, to prevent inadvertent expansion (optional)

keithwrightbos avatar Jun 20 '16 19:06 keithwrightbos

I think this could be really useful for other use cases as well, such as cross-context conversion ID propagation which would be needed in many e-commerce use cases. CC @rudygalfi

One issue is that URL replacement can be async which would block target=_blank emulation with window.open. Should we limit expansion to values that are available synchronously?

cramforce avatar Jun 20 '16 23:06 cramforce

Yes with the caveat that we are allowing for replacement of data received from async post message. There are additional issues to consider:

  1. How to handle later click listeners that may prevent navigation (i.e. a protection against accidental clicks). We do not want to prevent later replacement should a non-prevented click occur. I propose storing the original href in an attribute (i.e. data-original-href) such that later clicks will use its value for replacement.
  2. For clicks on elements within shadow DOM, the clientX/Y would ideally be offset from the shadow root instead of the page (this is to ensure parity with the contents having been placed within an iframe as is the case for #3133). We can handle this by allowing the CLICK_X/CLICK_Y to provide an additional argument that is the parent from which it should offset OR we can walk the ancestor tree and if there is a shadow root, then use it as the offset. The latter would force shadow root offset for all anchors which may not match all, future use cases. Preference?
  3. Target of click is not an anchor but does have an href (or other attribute which would like to have its value update). I propose for now that this usage case is not supported. In other words, replacement will only occur if the target is an anchor with an href attribute.

keithwrightbos avatar Jun 21 '16 14:06 keithwrightbos

@keithwrightbos upon click, do you need to wait for that postMessage to arrive, or should it have arrived already? If we do need to wait for the async operation, this would be problematic due to the window.open limitations described above.

cramforce avatar Jun 21 '16 14:06 cramforce

No, we do not need to wait. Idea is that dependency has been sending post message in advance of possible click.

keithwrightbos avatar Jun 21 '16 15:06 keithwrightbos

@keithwrightbos Cool. Yeah. Then we just need to create a sync mode, where only substitutions that can be fulfilled immediately are performed.

cramforce avatar Jun 21 '16 15:06 cramforce

@cramforce Great. Do you have an opinion about the offset issue identified in item 2 above?

keithwrightbos avatar Jun 21 '16 16:06 keithwrightbos

LGTM

cramforce avatar Jul 14 '16 18:07 cramforce

have we considered if POSTMESSAGE can be used to steal data sent from viewer? for example our base CID?

lannka avatar Jan 03 '17 23:01 lannka

POSTMESSAGE support is unique to amp-signal-collection-frame (PR #6830) at this time and as such in that PR I ensure that only messages from an iframe within amp-signal-collection-frame element will be allowed. Let's discuss offline.

On Tue, Jan 3, 2017 at 6:59 PM Hongfei Ding [email protected] wrote:

have we considered if POSTMESSAGE can be used to steal data sent from viewer? for example our base CID https://github.com/ampproject/amphtml/blob/master/src/service/viewer-impl.js#L731 ?

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/ampproject/amphtml/issues/3665#issuecomment-270260215, or mute the thread https://github.com/notifications/unsubscribe-auth/ABr0L1TOhF4Pyolol-0N5ceKQCt86-5pks5rOuDngaJpZM4I6D7U .

keithwrightbos avatar Jan 04 '17 14:01 keithwrightbos