Commit dcf6a1ab authored by Marcelo Vani's avatar Marcelo Vani Committed by Marcelo Vani
Browse files

Issue #2882682 by marcelovani: AMP Analytics

parent 10a952f1
......@@ -85,8 +85,8 @@ The body field presents a special problem, since it is likely to contain lots of
* Click Save button (this brings you back to the AMP config form)
### Analytics (optional)
* Enter your Google Analytics Web Property ID and click save
* This will automatically be added to the AMP version of your pages
* Go to the AMP configuration screen at `/admin/config/content/amp/analytics`
* Enter your Google Analytics Web Property ID and click save configuration
### Adsense (optional)
* Enter your Google AdSense Publisher ID and click save
......
......@@ -50,14 +50,6 @@ function amp_admin_form($form, &$form_state) {
'#description' => t('Choose a theme to use for AMP pages.'),
'#default_value' => variable_get('amp_theme', 'ampsubtheme_example'),
);
$form['amp_google_analytics_id'] = array(
'#title' => t('Google Analytics Web Property ID'),
'#type' => 'textfield',
'#default_value' => variable_get('amp_google_analytics_id'),
'#size' => 15,
'#maxlength' => 20,
'#description' => t('This ID is unique to each site you want to track separately, and is in the form of UA-xxxxxxx-yy. To get a Web Property ID, <a href="@analytics">register your site with Google Analytics</a>, or if you already have registered your site, go to your Google Analytics Settings page to see the ID next to every site profile. <a href="@webpropertyid">Find more information in the documentation</a>.', array('@analytics' => 'http://www.google.com/analytics/', '@webpropertyid' => url('https://developers.google.com/analytics/resources/concepts/gaConceptsAccounts', array('fragment' => 'webProperty')))),
);
if (!module_exists('amp_adsense')) {
$form['amp_google_adsense_id'] = array(
......@@ -174,14 +166,14 @@ function amp_admin_form($form, &$form_state) {
/**
* Submit handler for the amp_admin_form
* Clears the Drupal cache if the AMP Library warnings display checkbox value is changed
*
*
* @param type $form
* @param type $form_state
*/
function amp_admin_form_submit($form, &$form_state) {
if (variable_get('amp_library_warnings_display', false) !== $form_state['values']['amp_library_warnings_display']) {
cache_clear_all();
drupal_set_message(t('AMP Library debugging checkbox changed. Cleared cache.'));
drupal_set_message(t('AMP Library debugging checkbox changed. Cleared cache.'));
}
}
......@@ -191,12 +183,6 @@ function amp_admin_form_submit($form, &$form_state) {
* @see amp_admin_form_submit()
*/
function amp_admin_form_validate($form, &$form_state) {
// Validate the Google Analytics ID.
if (!empty($form_state['values']['amp_google_analytics_id'])) {
if (!preg_match('/^UA-\d+-\d+$/', $form_state['values']['amp_google_analytics_id'])) {
form_set_error('amp_google_analytics_id', t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'));
}
}
// Validate the Google Adsense ID.
if (!empty($form_state['values']['amp_google_adsense_id'])) {
if (!preg_match('/^pub-[0-9]+$/', $form_state['values']['amp_google_adsense_id'])) {
......
......@@ -55,7 +55,6 @@ function amp_requirements($phase) {
function amp_uninstall() {
// Delete variables.
variable_del('amp_theme');
variable_del('amp_google_analytics_id');
variable_del('amp_google_adsense_id');
variable_del('amp_google_doubleclick_id');
variable_del('amp_pixel');
......@@ -71,3 +70,11 @@ function amp_uninstall() {
variable_del('amp_metadata_organization_logo');
variable_del('amp_metadata_organization_logo_image_style_id');
}
/**
* Enable submodule amp_analytics.
*/
function amp_update_7001() {
// For backwards compatibility since the code has been moved to a submodule.
module_enable(array('amp_analytics'));
}
......@@ -62,6 +62,18 @@ function amp_menu() {
'type' => MENU_LOCAL_TASK,
'file' => 'amp.admin.inc',
);
$items['admin/config/content/amp/analytics'] = array(
'title' => 'AMP Analytics',
'description' => 'Configure AMP Analytics',
'page callback' => 'drupal_get_form',
'page arguments' => array('amp_analytics_admin_form'),
'access arguments' => array('administer site configuration'),
'type' => MENU_LOCAL_TASK,
'file' => 'modules/amp_analytics/amp_analytics.admin.inc',
'weight' => '3',
);
return $items;
}
......@@ -105,14 +117,6 @@ function amp_theme() {
),
'template' => 'templates/amp-ad'
),
'amp_analytics' => array(
'variables' => array(
'account' => NULL,
'analytics_attributes' => NULL,
'analytics_attributes_array' => array()
),
'template' => 'templates/amp-analytics'
),
'amp_iframe' => array(
'variables' => array(
'iframe' => NULL
......@@ -246,13 +250,6 @@ function amp_preprocess_amp_ad(&$variables) {
$variables['slot_attributes'] = drupal_attributes($variables['slot_attributes_array']);
}
/**
* Implements hook_preprocess_amp_analytics().
*/
function amp_preprocess_amp_analytics(&$variables) {
$variables['analytics_attributes'] = drupal_attributes($variables['analytics_attributes_array']);
}
/**
* Implements hook_preprocess_amp_video().
*/
......@@ -276,46 +273,6 @@ function amp_preprocess_amp_video(&$variables) {
$variables['video_attributes'] = drupal_attributes($variables['video_attributes_array']);
}
/**
* Implements hook_element_info().
*/
function amp_element_info() {
// AMP Analytics element
$types['amp_analytics'] = array(
'#account' => NULL,
'#analytics_attributes_array' => [],
'#pre_render' => array('amp_pre_render_amp_analytics'),
'#theme' => 'amp_analytics',
);
return $types;
}
/**
* #pre_render callback to render amp_analytics with js library added to head.
*
* @param $elements
* A render array for amp analytics elements.
*
* @return
* The passed-in element with the js library necessary for the amp-analytics
* element added to head.
*/
function amp_pre_render_amp_analytics($element) {
$head_js = array(
'#tag' => 'script',
'#type' => 'html_tag',
'#attributes' => array(
'src' => 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js',
'async' => "",
'custom-element' => 'amp-analytics'
),
);
drupal_add_html_head($head_js, 'amp-analytics');
return $element;
}
/**
* Implements hook_entity_info_alter().
*/
......@@ -602,18 +559,6 @@ function amp_view_modes_submit(&$form, $form_state) {
*/
function amp_page_alter(array &$page) {
if (amp_is_amp_request()) {
$google_analytics_id = variable_get('amp_google_analytics_id');
if (!empty($google_analytics_id)) {
$amp_analytics = array(
'#type' => 'amp_analytics',
'#account' => $google_analytics_id,
'#analytics_attributes_array' => array(
'type' => 'googleanalytics'
)
);
$page['page_bottom']['amp_analytics'] = $amp_analytics;
}
if (variable_get('amp_pixel')) {
$domain = variable_get('amp_pixel_domain_name');
$query_string = variable_get('amp_pixel_query_string');
......
......@@ -5,7 +5,7 @@
* Template for amp-analytics.
*
* Available variables:
* - account: The analytics account ID.
* - content: The json output.
* - analytics_attributes: The HTML attributes for amp-analytics, primarily:
* - type: The type of analytics account.
*
......@@ -14,16 +14,6 @@
?>
<amp-analytics <?php print $analytics_attributes; ?>>
<script type="application/json">
{
"vars": {
"account": "<?php print $account; ?>"
},
"triggers": {
"trackAmpview": {
"on": "visible",
"request": "pageview"
}
}
}
<?php print $content; ?>
</script>
</amp-analytics>
<?php
/**
* @file
* Administrative page callbacks for the AMP analytics module.
*/
/**
* Form constructor for the AMP analytics form.
*/
function amp_analytics_admin_form($form, &$form_state) {
$form['amp_analytics'] = array(
'#type' => 'fieldset',
'#title' => t('AMP Analytics'),
'#group' => 'additional_settings',
'#weight' => 100,
);
$form['amp_analytics']['amp_google_analytics_id'] = array(
'#title' => t('Google Analytics Web Property ID'),
'#type' => 'textfield',
'#default_value' => variable_get('amp_google_analytics_id'),
'#size' => 15,
'#maxlength' => 20,
'#description' => t('This ID is unique to each site you want to track separately, and is in the form of UA-xxxxxxx-yy. To get a Web Property ID, <a href="@analytics">register your site with Google Analytics</a>, or if you already have registered your site, go to your Google Analytics Settings page to see the ID next to every site profile. <a href="@webpropertyid">Find more information in the documentation</a>.', array('@analytics' => 'http://www.google.com/analytics/', '@webpropertyid' => url('https://developers.google.com/analytics/resources/concepts/gaConceptsAccounts', array('fragment' => 'webProperty')))),
);
return system_settings_form($form);
}
/**
* Form validation handler for amp_admin_form().
*
* @see amp_admin_form_submit()
*/
function amp_analytics_admin_form_validate($form, &$form_state) {
// Validate the Google Analytics ID.
if (!empty($form_state['values']['amp_google_analytics_id'])) {
if (!preg_match('/^UA-\d+-\d+$/', $form_state['values']['amp_google_analytics_id'])) {
form_set_error('amp_google_analytics_id', t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'));
}
}
}
name = AMP Analytics
description = Helps render Analytics elements
core = 7.x
dependencies[] = amp
files[] = amp_analytics.test
<?php
/**
* @file
* AMP Analytics submodule.
*/
function amp_analytics_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'amp_admin_form') {
}
}
/**
* Implements hook_theme().
*/
function amp_analytics_theme($existing, $type, $theme, $path) {
$theme = array(
'amp_analytics' => array(
'variables' => array(
'content' => NULL,
'analytics' => NULL,
'analytics_attributes' => NULL,
'analytics_attributes_array' => array()
),
'template' => 'amp-analytics'
),
);
return $theme;
}
/**
* Implements hook_preprocess_amp_analytics().
*/
function amp_analytics_preprocess_amp_analytics(&$variables) {
$variables['analytics_attributes'] = drupal_attributes($variables['analytics_attributes_array']);
$variables['analytics'] = array(
'vars' => array(
'account' => variable_get('amp_google_analytics_id'),
),
'triggers' => array(
'trackAmpview' => array(
'on' => 'visible',
'request' => 'pageview',
),
),
);
}
/**
* Implements hook_process_amp_analytics().
*/
function amp_analytics_process_amp_analytics(&$variables) {
$variables['content'] = json_encode($variables['analytics']);
}
/**
* Implements hook_element_info().
*/
function amp_analytics_element_info() {
// AMP Analytics element
$types['amp_analytics'] = array(
'#account' => NULL,
'#analytics_attributes_array' => [],
'#pre_render' => array('_amp_analytics_pre_render'),
'#theme' => 'amp_analytics',
);
return $types;
}
/**
* #pre_render callback to render amp_analytics with js library added to head.
*
* @param $elements
* A render array for amp analytics elements.
*
* @return
* The passed-in element with the js library necessary for the amp-analytics
* element added to head.
*/
function _amp_analytics_pre_render($element) {
$head_js = array(
'#tag' => 'script',
'#type' => 'html_tag',
'#attributes' => array(
'src' => 'https://cdn.ampproject.org/v0/amp-analytics-0.1.js',
'async' => "",
'custom-element' => 'amp-analytics'
),
);
drupal_add_html_head($head_js, 'amp-analytics');
return $element;
}
/**
* Implements hook_page_alter().
*/
function amp_analytics_page_alter(array &$page) {
if (amp_is_amp_request()) {
$google_analytics_id = variable_get('amp_google_analytics_id');
if (!empty($google_analytics_id)) {
$amp_analytics = array(
'#type' => 'amp_analytics',
'#analytics_attributes_array' => array(
'type' => 'googleanalytics'
)
);
$page['page_bottom']['amp_analytics'] = $amp_analytics;
}
}
}
/**
* Implements hook_uninstall().
*/
function amp_analytics_uninstall() {
variable_del('amp_google_analytics_id');
}
<?php
/**
* @file
* Tests for amp_analytics.module.
*/
class AmpAnalyticsTestCase extends DrupalWebTestCase {
protected $admin_user;
public static function getInfo() {
return array(
'name' => 'AMP Analytics',
'description' => 'Tests for the AMP Analytics module.',
'group' => 'AMP',
);
}
protected function setUp() {
// Enable AMP module.
parent::setUp('field_ui', 'amp_test', 'amp_analytics');
// Create Admin user.
$this->admin_user = $this->drupalCreateUser(array(
'administer content types',
'administer fields',
'administer site configuration',
)
);
$this->drupalLogin($this->admin_user);
// Install the AMP theme.
theme_enable(array('amptheme', 'ampsubtheme_example'));
}
/**
* Test AMP analytics.
*/
public function testAmpAnalytics() {
// Login as an admin user.
$this->drupalLogin($this->admin_user);
// Configure AMP analytics.
$this->drupalGet('admin/config/content/amp/analytics');
$this->assertResponse(200);
$edit = [
"amp_google_analytics_id" => 'UA-11111-1',
];
$this->drupalPost(NULL, $edit, t('Save configuration'));
// Create a node to test AMP metadata.
$node = $this->drupalCreateNode(array('type' => 'article'));
// Enable AMP display on article content.
$this->drupalGet("admin/structure/types/manage/article/display");
$this->assertResponse(200);
$edit = ["view_modes_custom[amp]" => '1'];
$this->drupalPost(NULL, $edit, t('Save'));
// Check the full display.
$this->drupalGet('node/' . $node->nid);
$this->assertResponse(200);
$this->assertNoRaw('<amp-analytics type="googleanalytics">');
// Check the AMP display.
$this->drupalGet('node/' . $node->nid, array('query' => array('amp' => TRUE)));
$this->assertResponse(200);
$this->assertRaw('<script src="https://cdn.ampproject.org/v0/amp-analytics-0.1.js" async="" custom-element="amp-analytics" />');
$this->assertRaw('<amp-analytics type="googleanalytics">');
$this->assertRaw('<script type="application/json">');
$this->assertRaw('{"vars":{"account":"UA-11111-1"},"triggers":{"trackAmpview":{"on":"visible","request":"pageview"}}}');
$this->assertRaw('</amp-analytics>');
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment