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 analytics 3p vendors, 3p side #10184

Closed
wants to merge 3 commits into from
Closed

Amp analytics 3p vendors, 3p side #10184

wants to merge 3 commits into from

Conversation

jonkeller
Copy link
Contributor

@jonkeller jonkeller commented Jun 28, 2017

Splits PR 9661 in two. This is the 3p lib side. The other side was PRed separately in #10185

Enables amp-analytics tag to use third-party vendors. These vendors' code will be run in a non-AMP iframe.

One-pager: https://docs.google.com/document/d/1mCW7HbQBWQONTWA2q_rKNJyTEdqGOyhX-x-ces4JHWc/edit#

dev().warn(TAG_, 'Attempted to send messages when no listener' +
' configured. Be sure to first call' +
' registerAmpAnalytics3pEventsListener()');
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@keithwrightbos You commented here in the previous PR. I addressed what you said, except an aside: "We could also try and recover from previous invalid/missing onNewAmpAnalyticsInstance here"
Copying over my response from the old PR:
Given the fix to your previous comment, it is no longer possible to reach here without implementing onNewAmpAnalyticsInstance, as the enclosing AmpAnalytics3pMessageRouter will now not have been created.
However, this could still happen if they have done the first line here but not the second:

      window.onNewAmpAnalyticsInstance = ampAnalytics => {
        ampAnalytics.registerAmpAnalytics3pEventsListener(events => {

Unfortunately we can't go and create that for them, so I just made the dev().warn message explicitly say "Attempted to send messages when no listener configured. Be sure to first call registerAmpAnalytics3pEventsListener()"

Copy link
Contributor

Choose a reason for hiding this comment

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

See previous comment that onNewAmpAnalyticsInstance should be called for each new creative message. I suggest moving onNewAmpAnalyticsInstance check into message receiver and if its not present, warn and do not create message router instance.

// Load the script specified in the iframe’s name attribute:
script = document.createElement('script');
script.src = 'https://3p.ampproject.net' +
JSON.parse(window.name).scriptSrc;
Copy link
Contributor Author

@jonkeller jonkeller Jun 29, 2017

Choose a reason for hiding this comment

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

@keithwrightbos Addressed your comment here from the previous PR, re: potential XSS exploit.
However, we don't/can't have getMode().localDev here yet. Would it be sufficient check whether document.location.host == 'localhost' in order to unbreak local testing? Or do you know of a better way?

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a slash after .net just to be safe. Ideally we would also ensure that window.now doesn't include something like scriptSrc: ':javascript()'}
Regarding local testing, I'm curious what @lannka thinks as it appears that remote HTML doesn't support it

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.
transport.js sends

getMode().localDev ? 'dist.3p/current/ampanalytics-lib.js' : '$internalRuntimeVersion$/ampanalytics-v0.js';

This file takes what was received and sets script.src like so:

script.src = (document.location.host == 'localhost' ? '/' : 'https://3p.ampproject.net/') + JSON.parse(window.name).scriptSrc;

Thus we are guaranteed that in the non-local case we have the full https://3p.ampproject.net/

@@ -317,6 +318,7 @@ var forbiddenTerms = {
'3p/iframe-messaging-client.js',
'3p/ampcontext.js',
'3p/ampcontext-integration.js',
'3p/ampanalytics-lib.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.

Note that this file is changed in both this PR and the accompanying 10185.

try {
window.onNewAmpAnalyticsInstance(router);
} catch (e) {
dev().error(TAG_, 'Exception thrown by onNewAmpAnalyticsInstance in' +
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe if you just include the error as another parameter AMP formats it correctly for reporting (e.g. dev().error(TAG_, 'Exception... ' + this.win_.location.href, e);)

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

}
}

if (window.onNewAmpAnalyticsInstance) {
Copy link
Contributor

Choose a reason for hiding this comment

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

OK so we now require that onNewAmpAnalyticsInstance exist BEFORE adding the library? It's not only getting called once per frame? That can't be correct... we want it to be called for every new amp-analytics type= instance encountered on the publisher page. The flow should be:

  • on new creative message check for onNewAmpAnalyticsInstance and if not present do not create router and warn. Note that you will get event messages. It's up to you how to handle them but I think its fine to just drop with warn message (perhaps worded indicated it may not have been able to be created)
  • create instance of message router and pass to onNewAmpAnalyticsInstance

*/
sendMessagesToListener(messages) {
if (!messages.length) {
dev().warn(TAG_, 'Attempted to send zero messages. Ignoring.');
Copy link
Contributor

Choose a reason for hiding this comment

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

Make sure messages indicate which frame failed the action as there could be multiple on the page

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

dev().warn(TAG_, 'Attempted to send messages when no listener' +
' configured. Be sure to first call' +
' registerAmpAnalytics3pEventsListener()');
}
Copy link
Contributor

Choose a reason for hiding this comment

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

See previous comment that onNewAmpAnalyticsInstance should be called for each new creative message. I suggest moving onNewAmpAnalyticsInstance check into message receiver and if its not present, warn and do not create message router instance.

// Load the script specified in the iframe’s name attribute:
script = document.createElement('script');
script.src = 'https://3p.ampproject.net' +
JSON.parse(window.name).scriptSrc;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a slash after .net just to be safe. Ideally we would also ensure that window.now doesn't include something like scriptSrc: ':javascript()'}
Regarding local testing, I'm curious what @lannka thinks as it appears that remote HTML doesn't support it

/**
* A class for holding AMP Analytics third-party vendors responses to frames.
*/
export class ResponseMap {
Copy link
Contributor

Choose a reason for hiding this comment

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

This should live in the other PR as it is not part of the client lib (I'm also hoping we can get rid of it completely in favor of a closure based solution...)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hmm, true. But the exports need to live in both PRs.

Copy link
Contributor

@keithwrightbos keithwrightbos left a comment

Choose a reason for hiding this comment

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

I also do not see any tests

@jonkeller
Copy link
Contributor Author

Closing this PR, replacing with https://github.com/google/amphtml/pull/20/files which simplifies things and switches to SubscriptionAPI.

@jonkeller jonkeller closed this Jul 14, 2017
@jonkeller jonkeller deleted the amp_analytics_3p_vendors_lib branch July 24, 2017 15:38
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.

3 participants