Amp Analytics uses a template to output the json on the page.

{
    "vars": {
      "account": "<?php print $account; ?>"
    },
    "triggers": {
      "trackAmpview": {
        "on": "visible",
        "request": "pageview"
      }
    }
  }

That has limitations.
Say I want to add extra tracking, I would have to clone the template and add the new parameters, i.e.

{  
   "vars":{  
      "account":"UA-31114-1"
   },
   "triggers":{  
      "default pageview":{  
         "on":"visible",
         "request":"pageview",
         "vars":{  
            "title":"Ten social media disasters of 2015"
         }
      },
      "trackClickOnHeader":{  
         "on":"click",
         "selector":"#visit-site",
         "request":"event",
         "vars":{  
            "eventCategory":"ui-components",
            "eventAction":"visit-site-click"
         }
      }
   },
   "extraUrlParams":{  
      "ds":"amp",
      "cd3":"People",
      "cd4":"20151229",
      "cd5":"Reaction",
      "cd8":"67412",
      "cd13":"20151229"
   }
}

Also, If I want different tracking on different pages, I would have to have many copies of the template

I would like to propose a flexible solution where we pass an array or object in hook_preprocess, meaning it can be altered by other modules.
And on the template, we just print the output json passed via hook_preprocess.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

marcelovani created an issue. See original summary.

marcelovani’s picture

Issue summary: View changes
Status: Active » Needs review
FileSize
16.21 KB
102.55 KB
171.24 KB

Attaching patch

Changes:
Moved all Analytics related code into a submodule, this made the code cleaner

New tab in AMP Config page

Updated the template to print the content

<amp-analytics <?php print $analytics_attributes; ?>>
<script type="application/json">
  <?php print $content; ?>
</script>
</amp-analytics>

Allow other modules to use hook_preprocess_amp_analytics() to alter the values of $variables['analytics'], example:

/**
 * Implements hook_preprocess_amp_analytics().
 */
function mymodule_amp_preprocess_amp_analytics(&$variables) {
  $node = menu_get_object();
  $analytics = array(
    'vars' => array(
      'account' => variable_get('amp_google_analytics_id'),
    ),
    'triggers' => array(
      'default pageview' => array(
        'on' => 'visible',
        'request' => 'pageview',
        'vars' => array(
          'title' => $node->title,
        )
      ),
      'trackClickOnHeader' => array(
        'on' => 'click',
        'selector' => '#visit-site',
        'request' => 'event',
        'vars' => array(
          'eventCategory' => 'ui-components',
          'eventAction' => 'visit-site-click'
        ),
      ),
    ),
    'extraUrlParams' => array(
      'ds' => 'amp',
    )
  );

  $variables['analytics'] = $analytics;
}

Added tests

ajclamp’s picture

Status: Needs review » Reviewed & tested by the community

I've downloaded and applied this patch and everything applied correctly. All the changes are present as specified, and AMP is working and creating AMP pages using the supplied templates.

marcelovani’s picture

Status: Reviewed & tested by the community » Fixed

We have been using this patch in production for a couple of months and it works fine. The analytics specific code was moved from the main module into the sub-module, making it look cleaner.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.