我有一个包含视频列表的网页。为了保持演示简单,我在演示中只使用了 2 个视频。我不想使用唯一的 HTML ID 属性引用每个视频,但我想跟踪在分析中播放了哪个 YouTube 视频。
跟踪所有 amp-youtube 标签但 VIDEO_STATE 仅引用单个实例的代码演示(仅用于测试)
在https://playground.amp.dev/上尝试以下代码
<!DOCTYPE html>
<html ⚡>
<head>
<meta charset="utf-8" />
<title>My AMP Page</title>
<link rel="canonical" href="self.html" />
<meta name="viewport" content="width=device-width" />
<style amp-boilerplate>body{-webkit-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-moz-animation:-amp-start 8s steps(1,end) 0s 1 normal both;-ms-animation:-amp-start 8s steps(1,end) 0s 1 normal both;animation:-amp-start 8s steps(1,end) 0s 1 normal both}@-webkit-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-moz-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-ms-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@-o-keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}@keyframes -amp-start{from{visibility:hidden}to{visibility:visible}}</style><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<script async custom-element="amp-youtube" src="https://cdn.ampproject.org/v0/amp-youtube-0.1.js"></script>
<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
<style amp-custom>
h1 {
margin: 1rem;
}
</style>
</head>
<body>
<amp-youtube data-videoid="mGENRKrdoGY" layout="responsive" width="480" height="270"></amp-youtube>
<br><br>
<amp-youtube id="test-youtube" data-videoid="9RTaIpVuTqE" layout="responsive" width="480" height="270"></amp-youtube>
<amp-analytics>
<script type="application/json">
{
"requests": {
"event": "https://postman-echo.com/get?ec=${eventCategory}&ea=${eventAction}&el=${eventLabel}"
},
"triggers": {
"Youtube_Video": {
"on": "video-play",
"request": "event",
"selector": "amp-youtube",
"vars": {
"eventCategory": "Video Play",
"eventAction": "play",
"eventLabel": "VIDEO_STATE(test-youtube, id)"
}
}
}
}
</script>
</amp-analytics>
</body>
</html>
我知道 VIDEO_STATE 的第一个参数需要是唯一的 HTML ID,但我需要对其进行编码,而不需要为每个视频创建大量触发器。