Description | Capture analytics data from an AMP document. |
Availability | Stable |
Required Script | <script async custom-element="amp-analytics" src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js"></script> |
Examples | analytics.amp.html |
The <amp-analytics>
element is used to measure activity on an AMP document. The details concerning what is measured and how that data is sent to an analytics server is specified in a JSON configuration object. It comes pre-configured to support many analytics vendors out of the box.
For example, the following <amp-analytics>
element is configured to send a request to https://example.com/analytics
when the document is first loaded, and each time an <a>
tag is clicked:
<amp-analytics>
<script type="application/json">
{
"requests": {
"pageview": "https://example.com/analytics?url=${canonicalUrl}&title=${title}&acct=${account}",
"event": "https://example.com/analytics?eid=${eventId}&elab=${eventLabel}&acct=${account}"
},
"vars": {
"account": "ABC123"
},
"triggers": {
"trackPageview": {
"on": "visible",
"request": "pageview"
},
"trackAnchorClicks": {
"on": "click",
"selector": "a",
"request": "event",
"vars": {
"eventId": "42",
"eventLabel": "clicked on a link"
}
}
}
}
</script>
</amp-analytics>
By specifying the name of an analytics vendor with the type
attribute you can quickly configure amp-analytics
to use the respective product. Additional configuration (such as your user id) may still be necessary.
Here's an example of usage of type
for a provider called XYZ:
<amp-analytics type="XYZ"> ... </amp-analytics>
Type attribute value: adobeanalytics
Adds support for Adobe Analytics. More details for adding Adobe Analytics support can be found at marketing.adobe.com.
Type attribute value: atinternet
Adds support for AT Internet. More details for adding AT Internet support can be found at developers.atinternet-solutions.com.
Type attribute value: burt
Adds support for Burt. Additionally, the trackingKey
variable must be specified. It's also possible to specify the optional variables category
and subCategory
.
Type attribute value: chartbeat
Adds support for Chartbeat. More details for adding Chartbeat support can be found at support.chartbeat.com.
Type attribute value: colanalytics
Adds support for ColAnalytics. Additionally, need the value for id
.
Type attribute value: clicky
Adds support for Clicky Web Analytics. More details for adding Clicky support can be found at clicky.com.
Type attribute value: comscore
Adds support for comScore Unified Digital Measurement™ pageview analytics. Requires defining var c2
with comScore-provided c2 id.
Type attribute value: cxense
Adds support for Cxense Insight analytics. Requires defining var siteId
with Cxense-provided siteId. More details can be found at wiki.cxense.com.
Type attribute value: googleanalytics
Adds support for Google Analytics. More details for adding Google Analytics support can be found at developers.google.com.
Type attribute value: infonline
Adds support for INFOnline / IVW. Requires a copy of amp-analytics-infonline.html on a different subdomain than the including AMP file (why?). The file must be served via HTTPS. Example: if your AMP files are hosted on www.example.com
, then amp-analytics-infonline.html
needs to be on another subdomain such as iframe.example.com
or assets.example.com
.
Additionally, the following variables must be defined:
st
: Angebotskennungco
: commentcp
: codeurl
: HTTPS location ofamp-analytics-infonline.html
More details for adding INFOnline / IVW support can be found at www.infonline.de.
Type attribute value: krux
Adds support for Krux. Configuration details can be found at help.krux.com.
Type attribute value: linkpulse
Adds support for Linkpulse. Configuration details can be found at docs.linkpulse.com
Type attribute value: lotame
Adds support for Lotame. More information and configuration details can be found at mylotame.force.com.
Type attribute value: mediametrie
Adds support for Médiamétrie tracking pages. Requires defining var serial
. Vars level1
to level4
are optional.
Type attribute value: mparticle
Adds support for mParticle. More details for adding mParticle support can be found at docs.mparticle.com.
Type attribute value: oewa
There is a variation called oewadirect
wich does not use iframe-ping solution - and has a better client detection, by using AMP CLIENT_ID, this is currently EXPERIMENTAL,
and as of today (01.07.2015) prohibited by the OEWA - as it does not use oewa2.js
Adds support for OEWA. Requires a copy of amp-analytics-oewa.html on a different subdomain than the including AMP file (why?). The file must be served via HTTPS. Example: if your AMP files are hosted on www.example.com
, then amp-analytics-oewa.html
needs to be on another subdomain such as oewa-amp.example.com
.
Additionally, the following variables must be defined:
in vars-section:
s
: offercp
: categorypath
in requests-section:
url
: HTTPS location ofamp-analytics-oewa.html
More details for adding ÖWA, support can be found here.
Type attribute value: parsely
Adds support for Parsely. Configuration details can be found at parsely.com/docs.
Type attribute value: piano
Adds support for Piano. Configuration details can be found at vx.piano.io.
Type attribute value: quantcast
Adds support for Quantcast Measurement. More details for adding Quantcast Measurement can be found at quantcast.com
Type attribute value: segment
Adds support for segment page views and events. To see the full list of fields that you can send, see Segment Spec.
Type attribute value: mpulse
Adds support for SOASTA mPulse. Configuration details can be found at docs.soasta.com.
Type attribute value: simplereach
Adds support for SimpleReach. Configuration details can be found at simplereach.com/docs
Type attribute value: snowplow
Adds support for Snowplow Analytics. More details for adding Snowplow Analytics support can be found at github.com/snowplow/snowplow/wiki.
Type attribute value: webtrekk
Adds support for Webtrekk. Configuration details can be found at supportcenter.webtrekk.com.
-
type
See Analytics vendors -
config
Optional attribute. This attribute can be used to load a configuration from a specified remote URL. The URL specified here should use https scheme. See alsodata-include-credentials
attribute below. The URL may include AMP URL vars.<amp-analytics config="https://example.com/analytics.config.json"></amp-analytics>
The response must follow the AMP CORS security guidelines.
-
data-credentials
Optional attribute. If set toinclude
turns on the ability to read and write cookies on the request specified viaconfig
above. -
data-consent-notification-id
Optional attribute. If provided, the page will not process analytics requests until an amp-user-notification with the given HTML element id is confirmed (accepted) by the user.
Configuration may be specified inline (as shown in the example above) or fetched remotely by specifying a URL in the
config
attribute. Additionally, built-in configuration for popular analytics vendors can be selected using
the type
attribute.
If configuration data from more than one of these sources is used, the configuration objects (vars, requests and triggers) will be merged together such that (i) remote configuration takes precedence over inline configuration and (ii) inline configuration takes precedence over vendor configuration.
The <amp-analytics>
configuration object uses the following format:
{
"requests": {
request-name: request-value,
...
},
"vars": {
var-name: var-value,
...
},
"triggers": {
trigger-name: trigger-object,
...
},
"transport": {
"beacon": *boolean*,
"xhrpost": *boolean*,
"image": *boolean*
}
}
The requests
attribute specifies the URLs used to transmit data to an analytics platform. The request-name
is used
in the trigger configuration to specify what request should be sent in response to a pariticular event. The request-value
is an https URL. These values may include placeholder tokens that can reference other requests or variables.
"requests": {
"base": "https://example.com/analytics?a=${account}&u=${canonicalUrl}&t=${title}",
"pageview": "${base}&type=pageview",
"event": "${base}&type=event&eventId=${eventId}"
}
amp-analytics
defines many basic variables that can be used in requests. A list of all such variables is available in the amp-analytics
Variables Guide. In addition, all of the variables supported by AMP HTML Substitutions Guide are also supported.
The vars
attribute in the configuration can be used to define new key-value pairs or override existing variables that can be referenced in request
values. New variables are commonly used to specify publisher specific information. Arrays can be used to specify a list of values that should be URL encoded separately while preserving the comma delimiter.
"vars": {
"account": "ABC123",
"countryCode": "tr",
"tags": ["Swift,Jonathan", "Gulliver's Travels"]
}
The triggers
attribute describes when an analytics request should be sent. It contains a key-value pair of trigger-name and
trigger-configuration. Trigger name can be any string comprised of alphanumeric characters (a-zA-Z0-9). Triggers from a
configuration with lower precedence are overridden by triggers with the same names from a configuration with higher precedence.
on
(required) The event to listener for. Valid values areclick
,scroll
,timer
, andvisible
.request
(required) Name of the request to send (as specified in therequests
section).vars
An object containing key-value pairs used to overridevars
defined in the top level config, or to specify vars unique to this trigger.selector
(required whenon
is set toclick
) This configuration is used on conjunction with theclick
trigger. Please see below for details.scrollSpec
(required whenon
is set toscroll
) This configuration is used on conjunction with thescroll
trigger. Please see below for details.timerSpec
(required whenon
is set totimer
) This configuration is used on conjunction with thetimer
trigger. Please see below for details.
Use this configuration to fire a request when the page becomes visible. The firing of this trigger can be configured using visibilitySpec
. If multiple properties are specified, they must all be true in order for a request to fire. Configuration properties supported in visibilitySpec
are:
selector
This property can be used to specify the element to which all thevisibilitySpec
conditions apply.continuousTimeMin
andcontinuousTimeMax
These properties indicate that a request should be fired when (any part of) an element has been within the viewport for a continuous amount of time that is between the minimum and maximum specified times. The times are expressed in milliseconds.totalTimeMin
andtotalTimeMax
These properties indicate that a request should be fired when (any part of) an element has been within the viewport for a total amount of time that is between the minimum and maximum specified times. The times are expressed in milliseconds.visiblePercentageMin
andvisiblePercentageMax
These properties indicate that a request should be fired when the proportion of an element that is visible within the viewport is between the minimum and maximum specified percentages. Percentage values between 0 and 100 are valid. Note that the lower bound (visiblePercentageMin
) is inclusive while the upper bound (visiblePercentageMax
) is not. When these properties are defined along with other timing related properties, only the time when these properties are met are counted.
In addition to the conditions above, visibilitySpec
also enables certain variables which are documented here.
"triggers": {
"defaultPageview": {
"on": "visible",
"request": "pageview",
"visibilitySpec": {
"selector": "#anim-id",
"visiblePercentageMin": 20,
"totalTimeMin": 500,
"continuousTimeMin": 200
}
}
}
Use this configuration to fire a request when a specified element is clicked. Use selector
to control which elements will cause this request to fire:
-
selector
A CSS selector used to refine which elements should be tracked. Use value*
to track all elements."triggers": { "anchorClicks": { "on": "click", "selector": "a", "request": "event", "vars": { "eventId": 128 } } }
Use this configuration to fire a request under certain conditions when the page is scrolled. This trigger provides special vars that indicate the boundaries that triggered a request to be sent. Use scrollSpec
to control when this will fire:
-
scrollSpec
This object can containverticalBoundaries
andhorizontalBoundaries
. At least one of the two properties is required for a scroll event to fire. The values for both of the properties should be arrays of numbers containing the boundaries on which a scroll event is generated. For instance, in the following code snippet, the scroll event will be fired when page is scrolled vertically by 25%, 50% and 90%. Additionally, the event will also fire when the page is horizontally scrolled to 90% of scroll width. To keep the page performant, the scroll boundaries are rounded to the nearest multiple of5
."triggers": { "scrollPings": { "on": "scroll", "scrollSpec": { "verticalBoundaries": [25, 50, 90], "horizontalBoundaries": [90] }, "request": "event" } }
Use this configuration to fire a request on a regular time interval. Use timerSpec
to control when this will fire:
-
timerSpec
Specification for triggers of typetimer
. The timer will trigger immediately and then at a specified interval thereafter.interval
Length of the timer interval, in seconds.maxTimerLength
Maximum duration for which the timer will fire, in seconds.
"triggers": { "pageTimer": { "on": "timer", "timerSpec": { "interval": 10, "maxTimerLength": 600 }, "request": "pagetime" } }
AMP Access system issues numerous events for different states in the access flow. See amp-access-analytics.md for details.
The transport
attribute specifies how to send a request. The value is an object with fields that
indicate which transport methods are acceptable.
beacon
Indicatesnavigator.sendBeacon
can be used to transmit the request. This will send a POST request, with credentials, and an empty body.xhrpost
IndicatesXMLHttpRequest
can be used to transmit the request. This will send a POST request, with credentials, and an empty body.image
Indicates the request can be sent by generating anImage
tag. This will send a GET request.
If more than one of the above transport methods are enabled, the precedence is beacon
> xhrpost
> image
. Only one transport method will be used, and it will be the highest precedence one that is permitted and available. If the client's user agent does not support a method, the next highest precedence method enabled will be used. By default, all three methods above are enabled.
In the example below, beacon
and xhrpost
are set to false
, so they will not be used even though they have higher precedence than image
. image
would be set true
by default, but it is explicitly declared here. If the client's user agent supports the image
method, then it will be used; otherwise, no request would be sent.
"transport": {
"beacon": false,
"xhrpost": false,
"image": true
}
The extraUrlParams
attribute specifies additional parameters to append to the query string of a request URL via the usual "&foo=baz" convention.
Here's an example that would append &a=1&b=2&c=3
to a request:
"extraUrlParams": {
"a": "1",
"b": "2",
"c": "3"
}
The extraUrlParamsReplaceMap
attribute specifies a map of keys and values that act as parameters to String.replace() to preprocess keys in the extraUrlParams configuration. For example, if an extraUrlParams
configuration defines "page.title": "The title of my page"
and the extraUrlParamsReplaceMap
defines "page.": "_p_"
, then &_p_title=The%20title%20of%20my%20page%20
will be appended to the request.
extraUrlParamsReplaceMap
is not required to use extraUrlParams
. If extraUrlParamsReplaceMap
is not defined, then no string substitution will happens and the strings defined in extraUrlParams
are used as-is.
See amp-analytics rules in the AMP validator specification.