Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sabrysuleiman/e42e238afb77410d10cdb6566edfb154 to your computer and use it in GitHub Desktop.
Save sabrysuleiman/e42e238afb77410d10cdb6566edfb154 to your computer and use it in GitHub Desktop.
enhanced data layer for your WordPress website
To create an enhanced data layer for your WordPress website, you can follow these steps:
Install and activate a Google Tag Manager (GTM) plugin:
First, you need to install a GTM plugin to your WordPress site. A popular choice is the Google Tag Manager for WordPress by Thomas Geiger. You can install and activate it from your WordPress dashboard by going to Plugins > Add New and searching for "Google Tag Manager for WordPress".
Set up a Google Tag Manager account:
If you don't already have a GTM account, sign up and create a new container for your website.
Configure the GTM plugin:
In your WordPress dashboard, go to Settings > Google Tag Manager and enter your GTM container ID (e.g., GTM-XXXXXX). Follow the plugin's instructions to properly configure the settings.
Create custom dataLayer variables:
Now you can create custom dataLayer variables to capture specific data points from your website. For example, if you want to track author names, categories, and tags for a blog post, you can add the following code to your theme's functions.php file:
function enhanced_data_layer() {
if ( is_single() ) {
global $post;
$author = get_the_author_meta( 'display_name', $post->post_author );
$categories = get_the_category();
$category_names = array();
foreach( $categories as $category ) {
$category_names[] = $category->name;
}
$tags = get_the_tags();
$tag_names = array();
if ( $tags ) {
foreach( $tags as $tag ) {
$tag_names[] = $tag->name;
}
}
?>
<script>
window.dataLayer = window.dataLayer || [];
dataLayer.push({
'postAuthor': '<?php echo esc_js( $author ); ?>',
'postCategory': <?php echo wp_json_encode( $category_names ); ?>,
'postTags': <?php echo wp_json_encode( $tag_names ); ?>
});
</script>
<?php
}
}
add_action( 'wp_head', 'enhanced_data_layer', 20 );
This code will push the author, category, and tag information to the dataLayer on single post pages.
Create GTM variables:
In your GTM account, go to Variables > New and create new Data Layer Variables with the same names you used in the previous step (e.g., postAuthor, postCategory, and postTags). Make sure the Data Layer Variable Name field matches the names you used in your dataLayer.push() function.
Create GTM triggers and tags:
Create triggers and tags in GTM to utilize your new dataLayer variables. For example, you can create a Google Analytics tag to send custom dimensions based on the author, category, and tag information.
Remember to publish your changes in GTM and test your implementation to ensure that the enhanced data layer is working correctly.
By following these steps, you can create an enhanced data layer for your WordPress site and use it to track and analyze more specific data points about your content and user interactions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment