In today’s post, we’re exploring the capabilities of MailPoet’s recent feature: the custom trigger. This feature allows you to initiate automations with your custom code, opening doors for more tailored subscriber experiences. Instead of relying on generic triggers, you now have the flexibility to design triggers that align perfectly with your business goals and strategies.
In this blog post we will demonstrate the power of this trigger with an example: We will setup an automation aimed at rewarding new Mastodon followers with a coupon code. This not only showcases the custom trigger’s utility but also offers a practical incentive for increasing your Mastodon following.
An Overview of Mastodon
For those unfamiliar, Mastodon is an open-source, decentralized platform similar to Twitter. Unlike a centralized setup, Mastodon operates on a federated system of multiple independent servers or “instances”. This unique system gives users increased control, enabling them to select or create instances that match their preferences. Mastodon stands out due to its emphasis on user privacy and absence of algorithm-driven content.
Linking WordPress with Mastodon
Mastodon operates using the ActivityPub protocol. Simply put, this protocol allows users from different social networks to connect if both platforms support it. But it also allows us to connect our WordPress installation to the network. To establish this connection, we’ll be using the ActivityPub plugin by Matthias Pfefferle. Once activated, Mastodon users can follow your blog. Every time you post a new article, your followers will see this post in their timeline and they can interact with it. And of course, we want to be in that feed!
Setting Up the Automation
To encourage users to follow you on Mastodon, we’re offering them lets say a 10% coupon. This coupon is automatically sent to subscribers when they press the “Follow” button.
Here’s our automation setup: We utilize the “Custom trigger” and direct it to respond to the mastodon_new_follower
hook. When this hook is fired, the automation sends out the coupon code to the customer who started following us.
Implementing the Custom Trigger
Let’s delve into the logic behind this. This part is a bit technical and assumes some familiarity with WordPress development. The process consists of two main steps:
In a first step, we need to somehow store the Mastodon URLs of our customers. This information is needed later to know exactly which customer started to follow us. Therefore we want to give our customers the possibility to enter their Mastodon profile URL during checkout and in their account. WooCommerce has a filter, we can utilize for that purpose: woocommerce_billing_fields
:
add_filter( 'woocommerce_billing_fields', function($fields) { $fields['mastodon'] = array( 'type' => 'url', 'label' => __( 'Your Mastodon URL', 'mastodon-automation' ), 'required' => false, 'placeholder' => esc_attr__( 'https://mastodon.social/@<username>', 'mastodon-automation' ), 'autocomplete' => 'mastodon', ); return $fields; } );
Now that we store this information, we can tap into an event of the ActivityPub plugin. When a user follows our blog, Mastodon notifies our WordPress installation. The ActivityPub plugin triggers the activitypub_followers_post_follow
action in response to which we will listen in our small custom plugin:
add_action( 'activitypub_followers_post_follow', function($actorUrl, $object, $userId, \Activitypub\Model\Follower $follower) { /** * @var \WP_User[] $users */ $users = (new \WP_User_Query([ 'meta_query' => [ [ 'meta_key' => 'mastodon', 'meta_value' => $follower->get_url(), ] ] ]))->get_results(); if (! $users) { return; } foreach ($users as $user) { if (! is_email($user->user_email)) { continue; } do_action('mastodon_new_follower', $user->user_email); } }, 10, 4 );
In essence, the Follower object provides the URL to the profile of the new follower through get_url()
. We then attempt to match this URL with those our users provided during checkout. When a match is found, the mastodon_new_follower
action is activated, which provides the email address of our customer. As you can see, this is exactly the action we told our custom trigger to listen to; So once a match is found, the automation is triggered.
Conclusion
This is just one example of the potential of MailPoet’s new custom triggers. With a bit of code, you can craft unique automations that cater to your audience’s needs. And that is not all, you can extend your automations even with custom steps, which will fire hooks instead of listening to them. For a deep dive into the technical aspects of our custom trigger and the custom step, you should have a look into our knowledge base. We explain the technical details for the trigger here and for the custom step here.