Last active
January 11, 2024 15:02
-
-
Save MaximilianoRicoTabo/b3966c77de891177acd79771a5c9a75f to your computer and use it in GitHub Desktop.
Capture Unique Post Data as Custom Dimensions Using the Google Analytics Integration Add On
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 | |
/** | |
* Capture Unique Post Data as Custom Dimensions Using the Google Analytics Integration Add On | |
* | |
* title: Capture Unique Post Data as Custom Dimensions Using the Google Analytics Integration Add On | |
* layout: snippet | |
* collection: add-ons, pmpro-google-analytics | |
* category: dimensions | |
* link: TBD | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
/** | |
* Customize dimensions array to add unique post tags to pmpro-google-analytics. | |
* | |
* @param array $dimensions The default dimensions. | |
* @return array $return The customized dimensions array. | |
*/ | |
function my_pmproga4_default_custom_dimension( $dimensions ) { | |
//Change the post ID to the ID of your unique post | |
$my_unique_id = 287; | |
if ( is_single( $my_unique_id ) ) { | |
//change the post ID to the ID of your unique post | |
$my_unique_post = get_post( $my_unique_id ); | |
//Get the tag from the post | |
$posttags = get_the_tags( $my_unique_post ); | |
// Use wp_list_pluck to get an array of tag names | |
$tag_names = wp_list_pluck( $posttags, 'name' ); | |
$dimensions['tags'] = $tag_names; | |
} | |
return $dimensions; | |
} | |
add_filter( 'pmproga4_default_custom_dimension', 'my_pmproga4_default_custom_dimension' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment