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

Add support for monetization in iframes #100

Merged
merged 1 commit into from Dec 31, 2020
Merged

Conversation

vezwork
Copy link
Member

@vezwork vezwork commented Dec 30, 2020

Closes #82

Design Notes

The design for iframe web monetization tracking was outlined in #82, as discussed with @sharon-wang (and ❤️'d by @thejustinwalsh). Iframe web monetization tracking in Akita works as follow:

  • Following the spec, only iframes with the allow="monetization" attribute will count as monetized. If the iframe does not have this attribute, or the attribute is programmatically removed, the iframe will not be counted as being monetized.
    • NOTE: It is technically possible for a provider to send payments to an iframe without the allow="monetization" attribute. In this case, this PR tracks web monetization events even if the iframe does not have allow="monetization" because users will probably want to know if they are streaming payment even if it is not spec compliant, but I wouldn't expect that to happen
  • iframe monetization and time spent are tracked as part of the top-level page's AkitaOriginData.
    • A page is considered monetized by akita (and therefore the user's visit and time spent on the page is considered monetized) if it contains a valid payment pointer in a monetization meta tag, OR if any of the iframes on the page contain a valid payment pointer in a monetization meta tag.
    • Visits to, and time spent on, iframes are NOT tracked separately from the top level page.
    • iframe web monetization events are stored under the top level page's origin, NOT under the iframe's origin.
    • This avoids the problem of worrying about how payment is split between iframes on the page.
  • Nested monetized iframes are not supported by this PR (nested iframes are iframes in iframes).
  • A new field called iframeSrcOrigin will be added to AkitaPaymentPointerData to keep track of the origin of payment pointers inside of iframes. (actually not necessary right now, I will discuss with @sharon-wang if this should be added)

For example, https://defold.itch.io/climberz only contains monetization within an iframe (the iframe contains a web game). This is how it appears in Akita:
A screenshot of Akita displaying monetization info for defold.itch.io, a site with a monetized iframe
Note that the iframe's origin is https://v6p9d9t4.ssl.hwcdn.net/html/2866116/Climberz/index.html, but that origin is not tracked nor is it shown in the UI.

Implementation Notes

src/content_main.js has been split into three content scripts and moved to a new folder src/content_scripts/

  • content_origin.js handles time and visit tracking for the top level page. content_origin.js decides if the page is monetized based on the payment pointers on the page and in iframes on the page.
  • content_general.js runs both at the top level page (alongside content_origin.js), and in iframes (alongside content_iframe.js); it handles forwarding web monetization events into akita events.
  • content_iframe.js runs inside of each iframe and tracks and notifies content_origin.js of payment pointer changes in the iframe via the webBrowser.runtime message channel. Each content_iframe.js in each iframe is assigned a uuid from content_origin.js via postMessage so that content_origin.js can know which iframe the script is associated with when the script sends messages.

manifest.json includes these content scripts in a specific order: content_shared.js must be included before both content_main.js and content_iframe.js because it initializes constants and starts tracking monetization events, and content_main.js must be included before content_iframe.js because it sets a flag window.isTopLevel which lets content_iframe.js not run the code it contains if it is in the top level page.

Note that webBrowser.runtime.sendMessage is used by content_iframe.js to send payment pointers when they change to the top level page instead of postMessage because the messages it sends cannot be accessed by the page, so it should be a little safer. postMessage must be used for sending uuids to iframes because the uuids must be sent directly to the iframe element so content_main.js knows which iframe it assigned the uuid.

Testing Notes

@vezwork
Copy link
Member Author

vezwork commented Dec 30, 2020

TODO: rename content_main.js to content_origin.js. rename content_shared.js to content_general.js

Copy link
Member

@sharon-wang sharon-wang left a comment

Choose a reason for hiding this comment

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

Looks good! Just some nits and wording/doc suggestions

manifest.json Outdated Show resolved Hide resolved
src/background_script.js Outdated Show resolved Hide resolved
src/content_scripts/content_iframe.js Outdated Show resolved Hide resolved
src/content_scripts/content_iframe.js Outdated Show resolved Hide resolved
src/content_scripts/content_iframe.js Outdated Show resolved Hide resolved
src/content_scripts/content_origin.js Outdated Show resolved Hide resolved
src/content_scripts/content_origin.js Outdated Show resolved Hide resolved
src/content_scripts/content_origin.js Outdated Show resolved Hide resolved
src/content_scripts/content_origin.js Outdated Show resolved Hide resolved
src/content_scripts/content_origin.js Show resolved Hide resolved
@vezwork
Copy link
Member Author

vezwork commented Dec 30, 2020

@sharon-wang addressed your comments and tested. Let me know if you have any other comments, otherwise the PR is in good shape.

Copy link
Member

@sharon-wang sharon-wang left a comment

Choose a reason for hiding this comment

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

Tested these changes with https://defold.itch.io/climberz and it's working for me on Brave.

just noticed this one thing, probably leftover typo from before. But lgtm and ready to merge!

src/content_scripts/content_general.js Outdated Show resolved Hide resolved
@vezwork
Copy link
Member Author

vezwork commented Dec 31, 2020

💯

@vezwork vezwork merged commit 260877a into esse-dev:master Dec 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] iframe allow="monetization"
2 participants