-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
analytics-iframe-transport-remote-frame.html
39 lines (38 loc) · 1.63 KB
/
analytics-iframe-transport-remote-frame.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Requests Frame</title>
<script>
window.addEventListener('amp-iframeTransportClientCreated', () => {
/**
* To receive AMP analytics events in a third-party frame, you must
* pass a callback function to this method. The callback will be called
* when an event is received, and will be passed two parameters:
* @param {string} event A string of the format specified in the
* requests block of the amp-analytics JSON config
* @param {string} transportId An ID uniquely identifying which creative
* generated the event
*/
window.iframeTransportClient.onAnalyticsEvent(
(event, transportId) => {
// Now, do something meaningful with the AMP Analytics event
console.log('The page at: ' + window.location.href +
' has received an event: ' + event +
' from the creative with transport ID: ' + transportId);
});
});
// Load the script specified in the iframe’s name attribute:
const url = JSON.parse(window.name).scriptSrc;
if (url && url.startsWith('https://3p.ampproject.net/')) {
script = document.createElement('script');
script.src = url;
document.head.appendChild(script);
// The script will be loaded, and will call onNewAmpAnalyticsInstance()
} else {
console.warn('Received invalid URL - risk of XSS! ' + url);
}
</script>
</head>
<!-- The frame will not be visible, so there is no need for a body tag. -->
</html>