Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
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.
Comment | File | Size | Author |
---|---|---|---|
#2 | tab.png | 171.24 KB | marcelovani |
#2 | folder.png | 102.55 KB | marcelovani |
#2 | amp_analytics-2882682-2.patch | 16.21 KB | marcelovani |
|
Comments
Comment #2
marcelovaniAttaching 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
Allow other modules to use hook_preprocess_amp_analytics() to alter the values of $variables['analytics'], example:
Added tests
Comment #3
ajclamp CreditAttribution: ajclamp as a volunteer commentedI'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.
Comment #5
marcelovaniWe 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.