Sending data to two different CRMs (Intercom Example)

#Overview

In the last case study, we explored a solution for a customer who wanted WP Fusion to segment his site users into two different ActiveCampaign accounts, depending on their user role.

In this article, we’ll be dealing with a slightly different problem. This customer uses both Intercom and ActiveCampaign, with ActiveCampaign as his primary CRM (because of the lack of marketing automation features in Intercom).

To keep WP Fusion easy to learn and configure, we don’t natively support connecting your WordPress site to more than one CRM at a time. In fact it’s generally better for your business if you can keep all of your contact data in one system, instead of having it fragmented across several platforms.

#The objective

But there are some exceptions to the rule. In this case, the customer would like to use ActiveCampaign as their primary CRM, but also be able to simultaneously tag contacts in Intercom for certain events.

#The solution

As we discussed in the introduction to WP Fusion’s CRM API, all communication with your CRM is done via interfacing with the wp_fusion()->crm object. In this case, WP Fusion is configured to connect to ActiveCampaign.

But it’s also possible to create and interface with a secondary CRM instance by simply including the appropriate file and passing in your connection details.

You can see the finished code on GitHub here, or continue reading below for a more detailed explanation:

#Setting things up

define( 'INTERCOM_ACCESS_TOKEN', 'aG9tOjyIyZmMxZDc2X2YxMzBfNDBhZV9hOTVjXzRhZDRlNzBiZWMyMzoxOjA=' );

global $intercom;

At the top of the file, we have a place to define your Intercom access token. This will be used later when we first set up the Intercom CRM object.

We also declare $intercom as a global, so that it can be re-used across functions while only having to be initialized once.

#Initializing the Intercom CRM object and configuring the connection

function wpf_connect_to_intercom() {

	global $intercom;

	if( is_object( $intercom ) ) {
		return $intercom;
	}

	require_once WPF_DIR_PATH . 'includes/crms/intercom/class-intercom.php';

	$intercom = new WPF_Intercom;
	$intercom->get_params( INTERCOM_ACCESS_TOKEN );

	return $intercom;

}

This function is responsible for setting up the Intercom CRM object, so it can be used in the other functions. Here’s how it works:

  • The first thing we do is check the global $intercom variable. If it’s already been set up, then nothing more needs to be done.
  • If Intercom hasn’t been set up yet, we include WP Fusion’s Intercom integration class, and enable the API connection by providing it with your access token.
  • Now the Intercom CRM object is ready to go (it’s that easy!), and you have access to all the available methods we covered earlier when looking at the WP Fusion CRM API.

#A helper function to make it easier to deal with Intercom contact IDs

This second function isn’t essential, but since we’ll be applying tags to Intercom contacts, which have their own contact IDs, I’ve included it to help reduce the number of API calls.

By storing the contact IDs for any users with known Intercom contact records, we won’t have to look them up again when we want to apply tags.

function wpf_get_intercom_contact_id( $user_id ) {

	$contact_id = get_user_meta( $user_id, 'wpf_intercom_contact_id', true );
	
	if( ! empty( $contact_id ) ) {
		
		return $contact_id;

	} else {

		$intercom = wpf_connect_to_intercom();

		$user = get_user_by( 'id', $user_id );
		$contact_id = $intercom->get_contact_id( $user->user_email );

		if( ! is_wp_error( $contact_id ) && $contact_id != false ) {

			update_user_meta( $user_id, 'wpf_intercom_contact_id', $contact_id );
			return $contact_id;

		} else {

			return false;

		}

	}

}

Here’s what we’re doing:

  • First check the WordPress user’s meta data for an Intercom contact ID. If one is found, we can use it right away.
  • If there is no contact ID saved, then use the Intercom API to look up the user based on their email address.
  • If there were no errors, and a contact ID was found, then we update the user’s metadata for future usage.
  • If no contact exists, return false.

#Sending new user registrations to Intercom, in addition to ActiveCampaign

This next function shows how you can create new Intercom contacts at user registration, after they’ve been added to ActiveCampaign.

function wpf_add_to_intercom( $user_id, $contact_id, $post_data ) {

	// Get the Intercom CRM object
	$intercom = wpf_connect_to_intercom();

	// Check if there's already a contact record in Intercom
	$contact_id = $intercom->get_contact_id( $post_data['user_email'] );

	if( ! is_wp_error( $contact_id ) && $contact_id == false ) {

		// If there's no existing contact, let's create one

		// You have to manually specify Intercom internal fields here
		// since the WPF Settings >> Contact fields tab is configured for ActiveCampaign

		$contact_data = array(
			'email'		=> $post_data['user_email'],
			'name'		=> $post_data['first_name'] . ' ' . $post_data['last_name']
		);

		// "false" in the second parameter tells it not to use the field mapping set up in the WP Fusion settings
		$contact_id = $intercom->add_contact( $contact_data, false );

	}

	// Save the contact ID for later reference
	update_user_meta( $user_id, 'wpf_intercom_contact_id', $contact_id );

	// Now you can apply tags
	$intercom->apply_tags( array( 'Tag One', 'Tag 2' ), $contact_id );

}

add_action( 'wpf_user_created', 'wpf_add_to_intercom', 10, 3 );

This function is hooked to the 'wpf_user_created' action, which is triggered whenever a user registers on your site and a new CRM contact has been created (in this case in ActiveCampaign).

Here’s what we’re doing:

  • First we get the Intercom CRM object, either by creating it and configuring the access token, or by retrieving the existing CRM object from the global if it already exists.
  • Next, we use the wpf_get_intercom_contact_id() function (above) to see if we can get a contact ID for the new user. Even though they just registered on the site, they may already be in Intercom… so we check that to avoid creating duplicate contacts.
  • If there was no error in looking up the contact ID, and a contact ID doesn’t exist, we proceed to create a new contact:
    • Because WP Fusion is configured for ActiveCampaign, the settings under WP Fusion >> Contact Fields won’t be accurate here.
    • In this case I’ve used Intercom’s internal 'email' and 'name' fields to create the contact. But any contact data can be added in this way.
    • Finally, we utilize the Intercom CRM object to create the new contact, by calling $intercom->add_contact( $contact_data, false );
    • This will return the contact ID of the newly created Intercom contact.
  • After the contact has been created, we save the contact ID to their user meta for future use.
  • And now, with a contact ID available, you can apply any tags that you’d like to be added at registration.

#Applying tags in Intercom when they’re applied in ActiveCampaign

This function follows a similar format as the previous one, but this time we’re using the 'wpf_tags_applied' hook, which is fired whenever a tag has been applied by WP Fusion.

In this case, any tags that have been applied in ActiveCampaign will be applied simultaneously in Intercom.

function wpf_apply_tags_in_intercom( $user_id, $tags ) {

	$intercom = wpf_connect_to_intercom();

	$contact_id = wpf_get_intercom_contact_id( $user_id );

	if( $contact_id !== false ) {
		$intercom->apply_tags( $tags, $contact_id );
	}

}

add_action( 'wpf_tags_applied', 'wpf_apply_tags_in_intercom', 10, 2 );

Here’s what we’re doing:

  • As before, we’re either creating or retrieving an existing CRM object to make our requests to.
  • We look up the contact ID, using the wpf_get_intercom_contact_id() function from earlier.
  • If the user does have a contact ID in Intercom, we use the CRM object to apply the relevant tags.

#In summary

Obviously this is a very niche example, and it could be customized to be even more specific. For example, to only add contacts to Intercom when a Gravity Form is submitted, or to only tag contacts in Intercom when a lesson is marked complete.

But it hopefully demonstrates how WP Fusion, as a framework, can be extended to accommodate even the most complex business requirements. And the methods we’ve explored here should serve as a foundation for any more specific scenarios you’d like to implement.

And, importantly, WP Fusion gives you a system where these kinds of complex API operations can be executed with a minimal amount of custom code. If you return to view the full Gist on GitHub, you’ll see we did all of this in just 51 lines of code.

 

We really enjoy coming up with these kinds of solutions for our customers. If you’re interested in discussing implementation ideas for your own project, send us a message, we’d love to hear from you!

 

Was this helpful?