ℹ️

How to track WordPress categories and tags

When you use Friendly Automate's website tracking, pages visited are stored in the "History" for each of your contacts:

image

You can use this information to target contacts who are interested in certain topics or are at a certain point in their customer journey.

However, if you have a blog with a lot of content, this quickly becomes overwhelming. After all, you don't want to define the topics for thousands of individual pages in Friendly Automate.

Fortunately, good CMS systems like WordPress offer a remedy here. WordPress requires you to assign at least one category to each blog post. Furthermore, you can add as many tags as you want to each post in WordPress.

You can take advantage of this information in Friendly Automate.

By default, Friendly Automate's tracking code "only" captures the URL and title of a page. However, if you add a small code snippet to your WordPress theme, you will automatically capture the categories and tags from WordPress within Mautic.

Friendly Automate will then display them as "tags" for each contact:

image

You can then use these tags to target contacts who are interested in certain topics. For this you can use the tag filter in segments:

image

Here you can now select the WordPress categories and tags of the blogposts your contacts have visited!

image

How do you set up tracking of WordPress categories and tags?

You can set up this advanced tracking in two easy steps.

1. Make sure that the official Mautic plugin for WordPress is installed

image

Here you can find instructions on how to install the plugin:

🗺️How to use the WordPress plugin to add the tracking code for Friendly Automate and use shortcodes

2. Add the following code to your WordPress theme

Go to the directory of your active WordPress theme. It is located in the subfolder "wp-content/themes/" of your WordPress installation.

There should be a file called "functions.php". If this file does not exist, create a new file with this name. It is important that the content of this file starts with "<?php".

Now add the following code to the functions.php:

// Add WordPress post categories and tags to Friendly Automate / Mautic as tags

function wordpress_post_meta_mautic( $attrs ) {

		// remove filter so it is not execute twice (don't know, why that happens)
	remove_filter( 'wpmautic_tracking_attributes', 'wordpress_post_meta_mautic' );
 

		// check if current element is a regular blog post - only these have categories and tags
	if (is_singular( 'post' )) {

			// add WordPress categories as Mautic tags
		foreach(get_the_category() as $wp_category) {

			$tags[] = "wordpress-category-" . $wp_category->cat_name;

		} 

			// add WordPress tags as Mautic tags
		foreach(get_the_tags() as $wp_tag) {

			$tags[] = "wordpress-tag-" . $wp_tag->name;

		} 

			// clean Mautic tag names
		foreach ($tags as $key => $value) {

			$value = str_replace(' ', '-', $value); // Replaces all spaces with hyphens
			$value = strtr($value,array('Ä' => 'Ae','Ö' => 'Oe','Ü' => 'Ue', 'ä' => 'ae','ö' => 'oe', 'ü' => 'ue','ß' => 'ss')); // replace German umlauts with best equivalent
			$value = iconv("utf-8","ascii//TRANSLIT",$value); // replace all other umlauts using the locale setting from WordPress 
			$value = preg_replace('/[^A-Za-z0-9\-]/', '', $value); // Removes special characters
			$value = strtolower($value); // set to lowercase

			$tags[$key] = $value;

		}

			// convert tags array to string
		$tags_string = implode(",", $tags);

			// pass tags to the Mautic tracking code
		$attrs['tags'] = $tags_string;

	}

	return $attrs;

}

add_filter( 'wpmautic_tracking_attributes', 'wordpress_post_meta_mautic');

Done :)

Visitors to your WordPress website should now automatically have WordPress categories and tags assigned as tags in Friendly Automate.

🙂
Any questions? We are happy to help you via 📧 email or ☎️ phone.

Don’t have time? 👩‍💻 Contact us and we’ll implement and design for you.