Created
December 10, 2022 00:13
-
-
Save jerclarke/2b904e2d91b85e162a5115c8529a80d6 to your computer and use it in GitHub Desktop.
A simple demo wp plugin to show how I add a second AMP tracker to AIWP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: AIWP Jer's Filter Demo | |
* Plugin URI: https://jerclarke.org | |
* Description: Simple demo of Jer's filters for AIWP | |
* Author: Jer Clarke | |
* Version: 0.1 | |
* Author URI: https://jerclarke.org | |
* Text Domain: jer-demo | |
* Domain Path: /languages | |
*/ | |
$Jer_AIWP_AMP_Extra_Tracker_Demo = new Jer_AIWP_AMP_Extra_Tracker_Demo(); | |
class Jer_AIWP_AMP_Extra_Tracker_Demo { | |
public function __construct() { | |
add_action('amp_post_template_footer', array( $this, 'action_amp_post_template_footer_to_create_custom_aiwp_amp_trackers' ), 1 ); | |
add_action('aiwp_analytics_amp_config', array($this, 'action_aiwp_analytics_amp_config')); | |
} | |
/** | |
* Create AIWP AMP objects for our custom tracker(s) and mark them to be filtered later | |
* | |
* AIWP only supports one tracker and tracker object, which automatically has the | |
* measurement_id configured in the plugin. | |
* | |
* We hack around this by creating a second object right before the tracker code is output, | |
* then reconfiguring it to use our measurement_id with filters during display | |
* | |
* Problem: We can't change AIWP_Tracking_GA4_AMP->measurement_id because it's `protected` | |
* | |
* Solution: We set ->gv_override_measurement_id property in the new object upon creation | |
* then in ->filter_aiwp_amp_config_to_replace_measurement_id_for_custom_trackers() we use | |
* the ->gv_override_measurement_id to update the config array that ultimately defines the | |
* JSON output. | |
* | |
* @return void | |
*/ | |
public function action_amp_post_template_footer_to_create_custom_aiwp_amp_trackers() { | |
$tracker_aiwp_amp = new AIWP_Tracking_GA4_AMP; | |
$tracker_aiwp_amp->gv_override_measurement_id = "G-8KG160R08Q"; | |
} | |
/** | |
* Filter the AIWP AMP tracker object to insert our custom configuration | |
* | |
* The AIWP AMP object we filter here has a ->config array which is ultimately converted | |
* into JSON to feed into the Thyngster/google-analytics-4-for-amp <amp-analytics> setup. | |
* | |
* We must use ->get() and ->set() because the ->config property is `protected` | |
* | |
* @param AIWP_Tracking_GA4_AMP $aiwp_amp | |
* @return void | |
*/ | |
public function action_aiwp_analytics_amp_config(AIWP_Tracking_GA4_AMP $aiwp_amp) { | |
$config = $aiwp_amp->get(); | |
$config = $this->filter_aiwp_amp_config_to_replace_measurement_id_for_custom_trackers($config, $aiwp_amp); | |
$aiwp_amp->set($config); | |
return; | |
} | |
/** | |
* Filter AIWP AMP tracker $config array for our custom tracker to insert our custom ->measurement_id | |
* | |
* @see $this->action_amp_post_template_footer_to_create_custom_aiwp_amp_trackers() docs for more details | |
* | |
* Thyngster docs describing how to work with the config JSON array: | |
* @link https://www.thyngster.com/how-to-track-amp-pages-with-google-analytics-4 | |
* | |
* @param array $config from AIWP_Tracking_GA4_AMP->get() | |
* @param AIWP_Tracking_GA4_AMP $aiwp_amp | |
* @return void | |
*/ | |
public function filter_aiwp_amp_config_to_replace_measurement_id_for_custom_trackers($config, AIWP_Tracking_GA4_AMP $aiwp_amp) { | |
// Only filter AIWP AMP trackers that we created and inserted a custom measurement_id into | |
if (!isset($aiwp_amp->gv_override_measurement_id)) { | |
return $config; | |
} | |
// We require this later, and if it's not here, the plugin is configured in a way we're not expecting | |
if (!isset($config['vars']['GA4_MEASUREMENT_ID'])) { | |
gv_error("ERROR GV_GA_Manager->filter_aiwp_amp_config_to_replace_measurement_id_for_custom_trackers() didn't find "); | |
return $config; | |
} | |
/** | |
* There are two places where the measurement_id is stored: | |
* | |
* "vars": { | |
* "GA4_MEASUREMENT_ID": "G-8KG160R08Q", | |
* ... | |
* "config": { | |
* "G-8KG160R08Q": { | |
* "groups": "default" | |
* } | |
* } | |
* }, | |
* | |
* We update the first one directly, then replace the second one using | |
* the old value of the first one and the array. | |
* | |
* Hopefully this will ensure it behaves exactly the same as the main tracker | |
*/ | |
$new_config = $config; | |
$new_config['vars']['GA4_MEASUREMENT_ID'] = $aiwp_amp->gv_override_measurement_id; | |
// Remove and back up the old config sub-array | |
$previous_measurement_id = $config['vars']['GA4_MEASUREMENT_ID']; | |
if (isset($config['vars']['config'][$previous_measurement_id])) { | |
$config_array = $config['vars']['config'][$previous_measurement_id]; | |
unset($new_config['vars']['config'][$previous_measurement_id]); | |
// Re-insert the old config sub-array but with the new measurement_id as it's key | |
$new_config['vars']['config'][$aiwp_amp->gv_override_measurement_id] = $config_array; | |
} | |
return $new_config; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment