- 谷歌AMP教程
- Google AMP - 主页
- Google AMP - 概述
- 谷歌 AMP - 简介
- Google AMP - 图片
- Google AMP - 表单
- Google AMP - Iframe
- Google AMP - 视频
- Google AMP - 按钮
- Google AMP - Timeago
- Google AMP - 数学
- Google AMP - 适合文本
- Google AMP - 日期倒计时
- Google AMP - 日期选择器
- Google AMP - 故事
- Google AMP - 选择器
- Google AMP - 链接
- Google AMP - 字体
- Google AMP - 列表
- Google AMP - 用户通知
- Google AMP - 下一页
- Google AMP - 属性
- 样式和自定义 CSS
- Google AMP - 动态 CSS 类
- Google AMP - 操作和事件
- Google AMP - 动画
- Google AMP - 数据绑定
- Google AMP - 布局
- 谷歌 AMP - ADS
- Google AMP - 分析
- Google AMP - 社交小部件
- Google AMP - 媒体
- Html 页面到 Amp 页面
- Google AMP - 基本语法
- Google AMP - 验证
- Google AMP - 缓存
- Google AMP - 自定义 Javascript
- Google AMP - Cors
- Google AMP 有用资源
- Google AMP - 快速指南
- Google AMP - 有用的资源
- Google AMP - 讨论
Google AMP - 自定义 Javascript
在前面的章节中,我们学习了许多放大器组件。我们还看到,为了让每个组件正常工作,我们需要添加一个 javascript 文件。
例如,对于 amp-iframe 添加的脚本如下 -
<script async custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"> </script>
我们已将async添加到 script 标记中。这是 amp 的标准,因为它们异步加载所有 javascript 文件。添加了一个自定义元素属性,其中包含它所使用的组件的名称。
要使用任何 amp 组件(如果它不是核心 amp javascript 文件的一部分),必须按如上所示添加脚本。
我们大多习惯在页面内编写大量的javascript代码,并且还使用script标签包含javascript文件。
我们怎样才能在放大器中做到这一点?因此,AMP 不允许在外部编写任何脚本代码或加载脚本标签。
Amp 有自己的组件来处理应该由页面上添加的附加脚本完成的工作。这样做基本上是出于性能原因,以便更快地加载页面内容,并且不会让 JavaScript 延迟渲染或对 DOM 进行任何更改。
这是 AMP 根据其官方网站给出的脚本标签规范 -
除非类型是application/ld+json,否则禁止。(可以根据需要添加其他不可执行的值。)例外是加载 AMP 运行时的强制脚本标记和加载扩展组件的脚本标记。
这里显示了一个工作示例,我们可以在我们的 amp 页面中使用application/ld+json 。请注意,我们使用 type=”application/ld+json” 的脚本标签作为 amp-analytics 组件来触发跟踪器。
同样,我们可以根据需要在其他 amp 组件上使用 type=”application/ld+json” 的脚本标签。
例子
<!doctype html>
<html amp>
<head>
<meta charset = "utf-8">
<title>amp-analytics</title>
<script async src = "https://cdn.ampproject.org/v0.js">
</script>
<script async custom-element = "amp-analytics"
src = "https://cdn.ampproject.org/v0/amp-analytics-0.1.js">
</script>
<link rel = "canonical" href = "ampanalytics.html">
<meta name = "viewport" content = "width=device-width,
minimum-scale = 1,initial-scale = 1">
<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>
</head>
<body>
<h1>Google Amp - Analytics</h1>
<amp-analytics>
<script type = "application/json">
{
"requests": {
"event": "http://localhost:8080/googleamp/tracking.php?
user=test&account=localhost&event=${eventId}"
},
"triggers": {
"trackPageview": {
"on": "visible",
"request": "event",
"vars": {
"eventId": "pageview"
}
}
}
}
</script>
</amp-analytics>
</body>
</html>
当页面在浏览器中被点击时,跟踪器将被触发以进行综合浏览。可以在 Google 网络选项卡中看到,如下所示。
