-
Notifications
You must be signed in to change notification settings - Fork 383
Analytics
There are two options you can follow to include analytics tags in your posts.
The plugin defines an analytics option to enable the addition of amp-analytics in your posts. When the plugin is active, an AMP top-level menu appears in the Dashboard with one inner sub-menu called 'Analytics':

Selecting the Analytics sub-menu in the AMP options menu takes us to an Analytics Options entry page, where we can
define the analytics tags we want to have, by specifying the vendor type (e.g. Parsely), and the associated JSON
configuration.

Notice that the goal of this option of the plugin is to provide a simple mechanism to insert analytics tags; it provides very simple validation based solely on the validity of the JSON string provided. It is the users responsibility to make sure that the values in the configuration string and the vendor type used are coherent with the analytics requirements of their site . Please review the documentation in the AMP project and in AMPByExample.
The AMP Analytics options entry form provides a very simple validation feedback mechanism: if the JSON configuration string entered is invalid (i.e. not valid JSON), an error message (in red) is displayed below the title of the options window and the entry form is reloaded:

And, if the configuration provided is actually a valid JSON string, a success message (in green) is displayed at the top of the window below the title, and again the entry form is reloaded.

For more on the JSON configuration supplied for Google Analytics, see Adding Analytics to your AMP pages.
Alternatively, you can use the amp_post_template_analytics filter:
add_filter( 'amp_post_template_analytics', 'xyz_amp_add_custom_analytics' );
function xyz_amp_add_custom_analytics( $analytics ) {
if ( ! is_array( $analytics ) ) {
$analytics = array();
}
// https://developers.google.com/analytics/devguides/collection/amp-analytics/
$analytics['xyz-googleanalytics'] = array(
'type' => 'googleanalytics',
'attributes' => array(
// 'data-credentials' => 'include',
),
'config_data' => array(
'vars' => array(
'account' => "UA-XXXXX-Y"
),
'triggers' => array(
'trackPageview' => array(
'on' => 'visible',
'request' => 'pageview',
),
),
),
);
// https://www.parsely.com/docs/integration/tracking/google-amp.html
$analytics['xyz-parsely'] = array(
'type' => 'parsely',
'attributes' => array(),
'config_data' => array(
'vars' => array(
'apikey' => 'YOUR APIKEY GOES HERE',
)
),
);
$analytics['alexa'] = array(
'type' => 'alexametrics',
'attributes' => array(),
'config_data' => array(
'vars' => array(
'atrk_acct' => CUSTOMER_ACCOUNT_CODE,
'domain' => 'example.com',
),
),
);
return $analytics;
}Each analytics entry must include a unique array key and the following attributes:
-
type:(string)one of the valid vendors for amp-analytics. -
attributes:(array)any additional valid attributes to add to theamp-analyticselement. -
config_data:(array)the config data to include in theamp-analyticsscript tag. This isjson_encode-d on output.
Notice: Please also see the plugin documentation on amp-wp.org