amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

I2I: dynamic variable macros on amp-iframe[src]

Open massawho opened this issue 6 years ago • 7 comments

Summary

Enable dynamic variable macros (ie: PAGE_VIEW_ID) on amp-iframe src attribute.

Motivation

We are moving our in-house ads server to Google Ad Manager and I have to integrate creative information that is generates on-the-fly by Google with an in-house tracker that requires some set of specific requirements to track those ad impressions.

We display 4 creatives per page (3 from a same slot called Sponsor Listing and 1 from a different slot called Display Ad). Each creative has its own impression_id using %%CACHEBUSTER%% macro and I have to dispatch one ad impression event for each of them. However, along with the impression_id I have to add the page_view_id to my impression event, which is generated by an AMP macro in the parent page. Because of the nature of x-origin iframes, it's not trivial to share these variables.

I have considered:

  • Using window.context, but there is an issue from 2017 that is still in triage https://github.com/ampproject/amphtml/issues/12342 . That would be the best solution I could think of, but I can't have access to window.context at all from my creative.
  • Send a postMessage from my creative and listening to the message events, but I wasn't able to set a window.addEventListener in my amp-script. Related issue https://github.com/ampproject/worker-dom/issues/747
  • Combining both solutions above, I tried to have a mediator iframe to listen for message events from my creative and have access to window.context, but I also didn't have access to window.context.
  • Using amp-analytics linkers, but I also have no success with that. Google Ad Manager simply doesn't fire any event to my amp-analytics, which made me to abort this.
  • Create a mediator iframe and add an amp-script to postMessage to this mediator iframe. The mediator iframe would catch the message from the parent page and from the creative and once both information are avaiable, it would dispatch the ad impression event. However, my amp-script is able to find the amp-iframe tag, but I can't find the the iframe using document.querySelector, which I believe this is expectable since iframe is generated dynamically.
  • To have my mediator iframe to receive dynamic parameters and in the request I would dispatch events to my tracker (based on our current implementation, it's actually easier to fire events from server side). However amp-iframe attribute doesn't support dynamic variables. => This is my intention to implement

Additional context

We currently have similar behaviour happening on our current ads server, but because we pass json data instead of using amp-ad or amp-iframe, it's much straightfoward: we use an amp-list and call our ads server passing the macro as the page_view_id. From there, our server fires the events. Since we are moving from our ads server to Google Ad Manager, we need to keep the behaviour, but now we need to rely on client capabilities.

/cc @ampproject/wg-approvers @clawr @lannka @calebcordry @sebastianbenz

massawho avatar Jan 09 '20 13:01 massawho

@massa1240 thank you for the feature request with all these details.

IIUC, you want your creatives served from GAM to read PAGE_VIEW_ID. As you already noticed, the window.context API doesn't work because it is designed for ad tags but not creatives.

using amp-iframe to relay the message seems like an interesting work around, but it definitely abuses the purpose of amp-iframe. we should have a more straightforward support in amp-ad.

One thing you can already do is to convert your ad into AMPHTML ads, and you will immediately get access to PAGE_VIEW_ID via <amp-analytics> in the creative, together with other speed & security benefits of AMPHTML ads.

If that is not an option for you (for example, it does not support fancy interactive ad formats, such as a mini game), we will need to introduce creative facing APIs in amp-ad.

@ampproject/wg-ads

lannka avatar Jan 09 '20 17:01 lannka

Hi @lannka ! Thanks for your reply

but it definitely abuses the purpose of amp-iframe. we should have a more straightforward support in amp-ad.

I totally agree with this statement, which made me not feel very comfortable implementing this solution in the beginning... due the circunstances, I decided to give this a shot, as it was the solution that made me closer to achieve what I had to.

Something I have achieved tho was passing a send-intersections postMessage and listen it back in my creative. If we had a similiar functionality to fetch amp metadata, would be awesome!

One thing you can already do is to convert your ad into AMPHTML ads, and you will immediately get access to PAGE_VIEW_ID via in the creative, together with other speed & security benefits of AMPHTML ads.

I actually tried this (I will be giving another shot to make sure I am not crazy here), but this didn't work. Whenever I place an <amp-analytics> in my creative, it doesn't get fired. I also filled a support question on google forum (https://support.google.com/admanager/thread/24272321?hl=en), got some upvotes but no answers so far. Same happens for amp-pixel. Using amp-img could possibly do the trick but I also can't use dynamic variables on it. Just one question here: if I place a PAGE_VIEW_ID inside my creative, will it be the same as the PAGE_VIEW_ID in my parent? I thought it wouldn't be since it comes from a new request, so it would generate its own value, and I will have more than one creative in the same page that all must share the same PAGE_VIEW_ID. Another attempt I gave a shot was firing amp-analytics to my in-house service using linkers, but so far I got nowhere. Caleb suggested to push my code to a working environment, since I am using localhost to make tests and this could be possibly bugging the code as I am using different ports (for different docker containers/web services). I will continue on his suggestion and see if I can reach somewhere, but I am not so confident as I have tried using amp-analytics before in my creative.

massawho avatar Jan 10 '20 06:01 massawho

@massa1240 I just tested on my side, and could confirm that the child PAGE_VIEW_ID is the same as parent's when both are firing through amp-pixel. What is your setting? Are they cross domain?

powerivq avatar Jan 17 '20 21:01 powerivq

Hi, @powerivq just a quick question, are you using iframe for this test? Because it could be slightly different from what I am testing. My ultimate goal is to send impression events from my Google DFP creative, so yes, they are cross-domain. However, any amp-analytics or amp-pixel I place on my creative doesn't get fired at all. I haven't tested firing pixels from an iframe because it doesn't actually help as I still have to fetch some data from my creative before sending the pixel...

massawho avatar Jan 19 '20 08:01 massawho

@massa1240 I am using <amp-ad type=fake> to include an AMPHTML ads that has amp-pixel and amp-analytics on it. The parent page also has amp-analytics and amp-pixel.

I tried both cross domain and same domain, both are able to send out the same PAGE_VIEW_ID.

powerivq avatar Jan 21 '20 19:01 powerivq

@massa1240 do you have a demo that we can play with?

lannka avatar Jan 21 '20 19:01 lannka

after a long discussion, we decided that this is not the best approach to deal with this issue. I will be writing a new I2C that would solve the same problem in a proper way using amp-ad

massawho avatar May 06 '20 06:05 massawho