Marketing Automation: Integrating with Pardot’s REST API

Companies use marketing automation to monitor and track the online behaviour of visitors to their site. That information is used to better understand how the visitor got to your information, at what point of the sales cycle their in and effectively decide whether to nurture them through email campaigns or pass their information over to sales. We can get a bit more in depth here, but let’s stick to coding.

In this post, I’ll cover the basics.

1) Capturing user details in a form.
2) Connecting to Pardot’s API.
3) Checking for a Prospect.
4) Creating a new Prospect.
5) Updating an existing Prospect.
6) Sending the data over to Pardot

How this whole thing works

For marketing automation to make sense and do it’s job, you need to be able to track a visitor to your site through cookies. With Pardot, much like Google or Marketo, you’ll have a script that needs to be added to your pages just above the closing body tag. This script will create the necessary cookies on the visitor’s computer and keep track of the pages they’ve visited, how they arrived to your site, and much more.

Capturing user details from a form

This section might me trivial. I thought it would be best to add it in here as we’re going to keep the store the visitor’s data in an array. In a following post, I’ll go over data validation – which should happen before this step. You’ll notice that I check for the existence of a value for each field before storing it to the array. Depending on your requirements, these fields could be mandatory.

	//Creating an array to store the values received from user form
	$pardotArray = array();
	
	//Checking for First Name and storing in array
	if (isset($_POST['first_name'])) {
		$pardotArray['first_name'] = trim($_POST['first_name']);
	}
	
	//Checking for Last Name and storing in array
	if (isset($_POST['last_name'])) {
		$pardotArray['last_name'] = trim($_POST['last_name']);
	}
	
	//Checking for Phone and storing in array
	if (isset($_POST['phone'])) {
		$pardotArray['phone'] = trim($_POST['phone']);
	}
	
	//Checking for Email Address and storing in array
	if (isset($_POST['email'])) {
		$pardotArray['email'] = trim($_POST['email']);
	}

	//Checking for Company and storing in array
	if (isset($_POST['company'])) {
		$pardotArray['company'] = trim($_POST['company']);
	}
	
	//Checking for Country and storing in array
	if (isset($_POST['country'])) {
		$pardotArray['country'] = trim($_POST['country']);
	}

Connecting to Pardot’s API

Pardot provides a RESTful API for customers to connect to. On connection, you get back an XML response. So, we’ll use PHP’s built in functionality to take the XML file and interpret it as an object.

//Connection Strings
$pardot_email = "YOUR_LOGIN_EMAIL_ADDRESS";
$pardot_pass = "YOUR_PASSWORD";
$pardot_user_key = "YOUR_USER_KEY";
$rest_domain = "YOUR_DOMAIN";

//Getting a fresh API Key from Pardot
$getAPIKey = "https://pi.pardot.com/api/login/version/3?email=".$pardot_email."&password=".$pardot_pass."&user_key=".$pardot_user_key;
$xml = simplexml_load_file($getAPIKey);
$pardot_api_key = $xml->api_key;

Checking for a Prospect

When a visitor fills out a form, before sending the visitor’s information over to Pardot, we have to know whether this is an existing person we know about (filled out a form previously or we have their email), or someone new. Like the connection to the API, this check will also return an XML response.

function checkProspect($rest_domain, $pardot_user_key, $pardot_api_key, $pardotArray['email']) {
	//Checking for a Prospect
	$prospectCheck = $rest_domain.'/api/prospect/version/3/do/read/email/'.$pardotArray['email'].'?user_key='.$pardot_user_key.'&api_key='.$pardot_api_key;
	$getProspect = simplexml_load_file($prospectCheck);

	if ($getProspect->prospect) {
		updateProspect($rest_domain, $pardot_user_key, $pardot_api_key, $pardotArray);
	} else {
		createProspect($rest_domain, $pardot_user_key, $pardot_api_key, $pardotArray);
	}
}

Creating a new Prospect

If our check for an existing prospect fails, we’ll proceed to creating a new prospect. Here we’ll pass the array that we created with the user’s submitted values, as well as the connection string to the API. Since I am using a separate function to send the data, this function simply prepares the data that we’ll be sending over to the Pardot camp.

//Function to create a new prospect
function createProspect($rest_domain, $pardot_user_key, $pardot_api_key, $pardotArray) {
	
	$pardotArrayEmail = $pardotArray['email'];
	$rest_function = 'create/email/'.$pardotArray['email'].'?';
	
	//Create the post arguments
	$pardot_string = "";
	foreach ($pardotArray as $key=>$value) { 
		$pardot_string .= $key.'='.$value.'&';
	}
	
	$postArgs = $pardot_string.'api_key='.$pardot_api_key.'&user_key='.$pardot_user_key;
	executeCurl($rest_domain, $rest_function, $postArgs, $pardotArrayEmail);
}

Updating an existing Prospect

So, in this scenario, we’ve found a prospect. Guess what? We’ll just have to update their record. This is the same process as creating a new prospect, but the Pardot link we’ll be calling is different and will initiate an update instead of a create.

//Function to update an existing prospect
function updateProspect($rest_domain, $pardot_user_key, $pardot_api_key, $pardotArray) {
	
	$pardotArrayEmail = $pardotArray['email'];
	$rest_function = 'update/email/'.$pardotArray['email'].'?';
	
	//Create the post arguments
	$pardot_string = "";
	foreach ($pardotArray as $key=>$value) { 
		$pardot_string .= $key.'='.$value.'&';
	}
	
	$postArgs = $pardot_string.'api_key='.$pardot_api_key.'&user_key='.$pardot_user_key;
	executeCurl($rest_domain, $rest_function, $postArgs, $pardotArrayEmail);
}

Sending the data over to Pardot

This is the final function. We’ve checked for the existence of the visitor and determined whether to create or update their record. Using the cURL function in PHP, we’ll send the HTTP request over to Pardot.

function executeCurl($rest_domain, $rest_function, $postArgs, $pardotArrayEmail) {
	
	// Pardot's REST web service
	$url = $rest_domain.'/api/prospect/version/3/do/'.$rest_function.'/'.$pardotArrayEmail.'?'; 
	
	// Initiate the curl session
	$curl = curl_init($url); 
	
	// Use an HTTP POST 
	curl_setopt($curl, CURLOPT_POST, true); 
	
	// We don't want to return the headers, just the response 
	curl_setopt($curl, CURLOPT_HEADER, false);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
	curl_setopt($curl, CURLOPT_POSTFIELDS, $postArgs); 
	
	// Make the REST call and close connection
	curl_exec($curl); 
	curl_close($curl);
}

Next Steps

Once you’ve submitted the data over, you’ll more than likely send your visitor over to a Thank You page. On this page, you’ll have to add a small bit of code from Pardot. Essentially, it is an IFRAME tag that calls your REST Domain and passes in the visitor’s email. I believe from Pardot’s end, this sort of ties in the visitor, the process and the cookie. It’s explained fully on their site.

I hope this post helped. In a further post, I’ll probably modify this code and convert it to a class.

3 Comments

  1. Dave Clements

    Hi Ahmed,

    I have a project where I’m trying to determine whether a user is a prospect in Pardot (as opposed to a visitor). This is to be done without any input from the user, so the plan is to take the visitor ID from the pardot cookie and then query the API to see whether they’re a prospect. Is this something you’re able to help with? I’d be willing to pay for you to complete this work.

    Thanks

  2. ahmeddirie

    Hi Dave,

    It’s been months since I’ve accessed Pardot. Regarding your question, I haven’t done this, but what you’re trying to do is most likely possible with an Ajax call to their API and passing in the visitor ID. You should be able to query the Visit object with the visitor ID. The object has a field for prospect_id and if the visitor is a prospect, you should see this information populated in the XML response.

    You’ll be making a call to
    /api/visit/version/3/do/query?

    Hope this helps.

  3. Ian McIntosh

    Hello Ahmed,

    Thank you so much for the example. Can you explain the last part a bit more (regarding the IFRAME tag)? We need to get the Activity information in Pardot, and we cannot complete this currently.

    -Ian

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>