Tagging WordPress and Woocomerce Users in Mautic

Wordpress Code

The official Wordpress plugin for Mauitc comes with a number of shortcodes, which makes it easy to add Mautic forms and content into posts and pages.

One of the shortcodes adds the ability to tag (or un-tag) visitors in Mautic depending on the pages they view. This makes it easy to tag visitors dynamically by using the shortcode within theme files.

This can be useful for many reasons. A couple that comes to mind are tagging users with the type of product that they viewed so follow up emails can be sent for similar products, or blog categories so you know which subjects each user is interested in.

The following technique can be used on any website by using the tracking pixel tag, but the following example code is specific to Wordpress.

It's possible to add tags to users within Mautic by using the tracking pixel with a comma separated list of tags. The tag looks like this:

<img src="https://mautic-domain.com/mtracking.gif?tags=Mautic,Wordpress" alt="Mautic Tags" />

The Wordpress shortcode just creates this image tag using the tags specified in the values attribute.

[mautictags values="Mautic,Wordpress"]

You can do something similar to the following on any site by creating the image tag rather than by using the shortcode, but I'm going to use the shortcode since it will reflect any changes made in the plugin to the tracking image should the syntax be updated in the future.

You can add any shortcode into a theme file using the do_shortcode function.

<?php echo do_shortcode('[mautictags values="Mautic,Wordpress"]'); ?>

So all we need to do is dynamically add the tags from the post content.

The easiest way I found was to use the wp_get_object_terms function. There are others to get tags and categories, but they all seem to be wrappers around this anyway, so we can use this to keep the code similar for all snippets. By default, it returns a lot of information, but we can specify that we only want the name field so it can be easily imploded to a comma separated string.

To tag users on a post page with categories, add to the templates single.php file:

global $post; 
$categories = implode(',', wp_get_object_terms( $post->ID, 'category', array( 'fields' => 'names' ) ));
echo do_shortcode('[mautictags values="'. $categories .'"]');

To tag with WordPress tags, we just need to change the second variable to get the tag taxonomy

global $post; 
$tags = implode(',', wp_get_object_terms( $post->ID, 'post_tag', array( 'fields' => 'names' ) ));
echo do_shortcode('[mautictags values="'. $tags .'"]');

For Woocommerce, the process is exactly the same, only the name of the taxonomy changes. For product categories, the following would go in a /woocommerce/single-product.php file in your theme:

global $post; 
$cats = implode(',', wp_get_object_terms( $post->ID, 'product_cat', array( 'fields' => 'names' ) ));
echo do_shortcode('[mautictags values="'. $cats .'"]');

The same for product tags:

global $post; 
$tags = implode(',', wp_get_object_terms( $post->ID, 'product_tag', array( 'fields' => 'names' ) ));
echo do_shortcode('[mautictags values="'. $tags.'"]');

You could also tag with the page title:

<?php echo do_shortcode('[mautictags values="'. get_the_title() .'"]'); ?>

A custom field:

$field = get_post_meta( get_the_ID(), 'field_name', true );
echo do_shortcode('[mautictags values="'. $field .'"]'); ?>

get_post_meta can really grab anything since most WordPress plugins store all additional info as post meta. You could get a product SKU or a custom field added manually to a post (Arguably more futureproof should you want to remove Mautic in the future and not want to remove all the shortcodes manually added to posts).

Once you have the tags within Mautic they can be used to build new segments.