-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathamp2.html
413 lines (402 loc) · 14.4 KB
/
amp2.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<!--
## Introduction
The `amp-analytics` element can be used to measure activity on an AMP document. Currently, `amp-analytics` supports different kinds of events:
* Pageview
* Anchor Clicks
* Timer
* Scrolling
* AMP Carousel changes
* Form
This sample demonstrates which events can be measured and how they can be configured. For a complete overview of all available options and parameters, have a look at the [official documentation](/content/amp-dev/documentation/components/reference/amp-analytics-v0.1.md).
--><!-- -->
<!doctype html>
<html ⚡>
<head>
<meta charset="utf-8">
<title>amp-analytics</title>
<script async src="https://cdn.ampproject.org/v0.js"></script>
<!-- ## Setup -->
<!-- Import the `amp-analytics` component in the header. -->
<script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script>
<script async custom-element="amp-carousel" src="https://cdn.ampproject.org/v0/amp-carousel-0.1.js"></script>
<script async custom-element="amp-bind" src="https://cdn.ampproject.org/v0/amp-bind-0.1.js"></script>
<script async custom-element="amp-form" src="https://cdn.ampproject.org/v0/amp-form-0.1.js"></script>
<!--
We need `amp-iframe` to embed an analytics dashboard.
-->
<script async custom-element="amp-iframe" src="https://cdn.ampproject.org/v0/amp-iframe-0.1.js"></script>
<link rel="canonical" href="https://amp.dev/documentation/examples/components/amp-analytics/index.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>
<style amp-custom>
:root {
--color-text-light: #fff;
--space-2: 1rem; /* 16px */
}
.fixed-dashboard {
background-color: #e0e0e0;
position: fixed;
bottom: 0;
right: 0;
width: 100%;
z-index: 100;
margin: var(--space-2);
box-shadow:0 1px 1px 0 rgba(0,0,0,.14),0 1px 1px -1px rgba(0,0,0,.14),0 1px 5px 0 rgba(0,0,0,.12);
}
amp-carousel .slide {
display: flex;
align-items: center;
justify-content: center;
font-size: 20px;
background: green;
color: var(--color-text-light);
}
</style>
</head>
<body>
<!-- -->
<amp-iframe class="fixed-dashboard" width="200" height="240" layout="fixed" sandbox="allow-scripts" frameborder="0" src="https://amp.dev/documentation/examples/components/amp-analytics/embed?test=1&user=2436b77e-24bd-49ed-a520-69be93f1aead&account=ampdev">
<amp-img src="/static/samples/img/pixel-white.png" layout="fixed-height" height="350" width="auto" placeholder></amp-img>
</amp-iframe>
<!--
## Pageview
-->
<!--
This is a simple `amp-analytics` configuration to fire a single request when the page becomes visible. Note how we declare a variable `eventId` in the request and define the concrete value in the `vars` block.
**Important: ** amp-analytics should be configured in the document body.
-->
<amp-analytics type="segment">
<script type="application/json">
{
"vars": {
"writeKey": "cN5V9esq84npFRiVBUYmAyPTWbJ6ngsY"
},
"requests": {
"event": "https://amp.dev/documentation/examples/components/amp-analytics/ping?user=2436b77e-24bd-49ed-a520-69be93f1aead&account=ampdev&event=${eventId}"
},
"triggers": {
"trackPageview": {
"on": "visible",
"request": "event",
"vars": {
"eventId": "pageview"
}
}
}
}
</script>
</amp-analytics>
<!-- ## UI elements appearance tracking -->
<!--
This is a more sophisticated view tracking. This sample fires an analytics request when a specific element becomes visible. The element is specified via `id` and must be an AMP element (amp-img, amp-iframe, amp-ad,...).
-->
<amp-analytics type="segment">
<script type="application/json">
{
"vars": {
"writeKey": "cN5V9esq84npFRiVBUYmAyPTWbJ6ngsY"
},
"requests": {
"event": "https://amp.dev/documentation/examples/components/amp-analytics/ping?user=2436b77e-24bd-49ed-a520-69be93f1aead&account=ampdev&event=${eventId}"
},
"triggers": {
"trackPageview": {
"on": "visible",
"request": "event",
"visibilitySpec": {
"selector": "#cat-image-id",
"visiblePercentageMin": 20,
"totalTimeMin": 500,
"continuousTimeMin": 200
},
"vars": {
"eventId": "catview"
}
}
}
}
</script>
</amp-analytics>
<!--
This image will trigger an analytics request, if at least 20% of it are visible for 500ms with a minimum time of 200ms continuous visibility.
-->
<amp-img id="cat-image-id" src="/static/samples/img/cat-looking-up-300x200.jpg" width="300" height="200" attribution="visualhunt" alt="a cat" layout="responsive"></amp-img>
<!--
This sample fires an analytics request when the image becomes visible as a result of an action.
-->
<div>
<button on="tap:an-image.toggleVisibility">
Toggle image
</button>
<amp-img hidden id="an-image" src="/static/samples/img/amp.jpg" width="1080" height="610" layout="responsive" alt="AMP">
</amp-img>
</div>
<!--
The `elementShown` event is issued when the image becomes visible -->
<amp-analytics type="segment">
<script type="application/json">
{
"vars": {
"writeKey": "cN5V9esq84npFRiVBUYmAyPTWbJ6ngsY"
},
"requests": {
"elementShown": "https://amp.dev/documentation/examples/components/amp-analytics/ping?user=2436b77e-24bd-49ed-a520-69be93f1aead&account=ampdev&event=${eventId}"
},
"triggers": {
"defaultPageview": {
"on": "visible",
"request": "elementShown",
"selector": "#an-image",
"vars": {
"eventId": "imageShownAfterButtonClick"
}
}
}
}
</script>
</amp-analytics>
<!-- ## Anchor click -->
<!--
User interactions can be tracked as well. This configuration will track clicks on any anchor links in the page.
-->
<amp-analytics type="segment">
<script type="application/json">
{
"vars": {
"writeKey": "cN5V9esq84npFRiVBUYmAyPTWbJ6ngsY"
},
"requests": {
"event": "https://amp.dev/documentation/examples/components/amp-analytics/ping?user=2436b77e-24bd-49ed-a520-69be93f1aead&account=ampdev&event=${eventId}"
},
"triggers": {
"trackAnchorClicks": {
"on": "click",
"selector": "a",
"request": "event",
"vars": {
"eventId": "clickOnAnyAnchor"
}
}
}
}
</script>
</amp-analytics>
<!-- Try it by clicking one of the links. -->
<ul>
<li><a>a link</a></li>
<li><a>another link</a></li>
</ul>
<!--
It is also possible to fire click events only for specific links. Select specific elements to tracking by specifying a `selector` id. This sample only tracks clicks on anchors with the id `anchor-id`.
-->
<amp-analytics type="segment">
<script type="application/json">
{
"vars": {
"writeKey": "cN5V9esq84npFRiVBUYmAyPTWbJ6ngsY"
},
"requests": {
"event": "https://amp.dev/documentation/examples/components/amp-analytics/ping?user=2436b77e-24bd-49ed-a520-69be93f1aead&account=ampdev&event=${eventId}"
},
"triggers": {
"trackAnchorClicks": {
"on": "click",
"selector": "#anchor-id",
"request": "event",
"vars": {
"eventId": "clickOnSpecialAnchor"
}
}
}
}
</script>
</amp-analytics>
<!-- Only one of these links will trigger the `clickOnSpecialAnchor` event. -->
<ul>
<li><a id="anchor-id">a special link</a>.</li>
<li><a id="another-anchor-id">a not so special link</a>.</li>
</ul>
<!-- ## Scroll event -->
<!-- Use scroll events to fire a request under certain conditions when the page is scrolled. -->
<amp-analytics type="segment">
<script type="application/json">
{
"vars": {
"writeKey": "cN5V9esq84npFRiVBUYmAyPTWbJ6ngsY"
},
"requests": {
"event": "https://amp.dev/documentation/examples/components/amp-analytics/ping?user=2436b77e-24bd-49ed-a520-69be93f1aead&account=ampdev&event=${eventId}"
},
"triggers": {
"scrollPings": {
"on": "scroll",
"scrollSpec": {
"verticalBoundaries": [10, 20, 30, 40, 50, 60, 70, 80, 90]
},
"request": "event",
"vars": {
"eventId": "scroll"
}
}
}
}
</script>
</amp-analytics>
<!-- ## Timer -->
<!--
Timer events fire in the specified interval.
-->
<amp-analytics type="segment">
<script type="application/json">
{
"vars": {
"writeKey": "cN5V9esq84npFRiVBUYmAyPTWbJ6ngsY"
},
"requests": {
"event": "https://amp.dev/documentation/examples/components/amp-analytics/ping?user=2436b77e-24bd-49ed-a520-69be93f1aead&account=ampdev&event=${eventId}"
},
"triggers": {
"pageTimer": {
"on": "timer",
"timerSpec": {
"interval": 10,
"maxTimerLength": 600
},
"request": "event",
"vars": {
"eventId": "timer"
}
}
}
}
</script>
</amp-analytics>
<!-- ## AMP Carousel Tracking-->
<!--
It is possible to track [amp-carousel](/content/amp-dev/documentation/components/reference/amp-carousel-v0.2.md) events with amp-analytics (this works only for `type="slides"`). Here is a sample carousel:
-->
<amp-carousel width="400" height="300" layout="responsive" type="slides">
<div class="slide" data-slide-id="slide1">Slide 1</div>
<div class="slide" data-slide-id="slide2">Slide 2</div>
<div class="slide">Slide 3</div>
</amp-carousel>
<!--
You can use the `fromSlide` and `toSlide` [variables](https://github.com/ampproject/amphtml/blob/master/extensions/amp-analytics/analytics-vars.md) for tracking which slides are viewed. The value is either taken from the `data-slide-id` attribute of the slide when present, else it represents the index of the slide (starting from 0).
-->
<!--
The `amp-carousel-change` event is issued when there is any change in the slide that is currently visible:
-->
<amp-analytics type="segment">
<script type="application/json">
{
"vars": {
"writeKey": "cN5V9esq84npFRiVBUYmAyPTWbJ6ngsY"
},
"requests": {
"event": "https://amp.dev/documentation/examples/components/amp-analytics/ping?user=2436b77e-24bd-49ed-a520-69be93f1aead&account=ampdev&event=${eventId}"
},
"triggers": {
"ampCarouselChange": {
"on": "amp-carousel-change",
"request": "event",
"vars": {
"eventId": "carousel-change-${toSlide}"
}
}
}
}
</script>
</amp-analytics>
<!--
The `amp-carousel-next` event is issued when there is a traversal to the next slide:
-->
<amp-analytics type="segment">
<script type="application/json">
{
"vars": {
"writeKey": "cN5V9esq84npFRiVBUYmAyPTWbJ6ngsY"
},
"requests": {
"event": "https://amp.dev/documentation/examples/components/amp-analytics/ping?user=2436b77e-24bd-49ed-a520-69be93f1aead&account=ampdev&event=${eventId}"
},
"triggers": {
"ampCarouselNext": {
"on": "amp-carousel-next",
"request": "event",
"vars": {
"eventId": "carousel-next-${toSlide}"
}
}
}
}
</script>
</amp-analytics>
<!--
The `amp-carousel-prev` event is issued when there is a traversal to the previous slide: -->
<amp-analytics type="segment">
<script type="application/json">
{
"vars": {
"writeKey": "cN5V9esq84npFRiVBUYmAyPTWbJ6ngsY"
},
"requests": {
"event": "https://amp.dev/documentation/examples/components/amp-analytics/ping?user=2436b77e-24bd-49ed-a520-69be93f1aead&account=ampdev&event=${eventId}"
},
"triggers": {
"ampCarouselPrev": {
"on": "amp-carousel-prev",
"request": "event",
"vars": {
"eventId": "carousel-prev-${fromSlide}"
}
}
}
}
</script>
</amp-analytics>
<!-- ## Form -->
<!-- You can track events triggered by `amp-form`. Find a list of supported event [here](/content/amp-dev/documentation/components/reference/amp-form.md#analytics-triggers)-->
<div>
<amp-analytics type="segment">
<script type="application/json">
{
"vars": {
"writeKey": "cN5V9esq84npFRiVBUYmAyPTWbJ6ngsY"
},
"triggers": {
"formSubmit": {
"on": "amp-form-submit",
"request": "event",
"selector":"#testForm",
"vars": {
"eventId": "form-submit"
}
},
"formSubmitSuccess": {
"on": "amp-form-submit-success",
"request": "event",
"selector":"#testForm",
"vars": {
"eventId": "form-submit-success"
}
},
"formSubmitError": {
"on": "amp-form-submit-error",
"request": "event",
"selector":"#testForm",
"vars": {
"eventId": "form-submit-error"
}
}
}
}
</script>
</amp-analytics>
<p>Try to insert any name as a name input in the form to see the <code>amp-form-submit-success</code> event.</p>
<p>Try to insert the word "error" as a name input in the form to see the <code>amp-form-submit-error</code> event.</p>
<form id="testForm" method="post" action-xhr="/documentation/examples/api/submit-form-input-text-xhr" target="_top">
<input type="text" name="name" placeholder="Name..." required>
<input type="submit" value="Subscribe">
</form>
</div>
</body></html>