Effective Olark API Use: Grabbing WordPress data

Thinking of adding the Olark live chat plugin to your Wordpress site? Good idea.

Today's guest post comes from Ashley, founder of Nose Graze, a specialty shop for Wordpess themes and plugins.

olark and wordpress.png

If you use live chat to talk to your customers (and I hope you do), it's easy to grab visitor information like login name and account email if they're logged into a WordPress account.

Here's how to do it:

On my site, Nose Graze, I sell digital products to my customers through WordPress and Easy Digital Downloads. Each customer gets their own WordPress account so they can see their purchase history. But when customers talked to me via the live chat, I always had to go through an extra step to get their login name or account email so I could look up their records.

I wanted a way to speed up my own process, and save my customers from having to enter the information themselves.

So I wrote the following API call to grab customer information and pull it through to my Olark chat dashboard. It's now easier and faster than ever for me to identify which customer I'm speaking to, and it doesn't require any extra effort on the customer's part.

API Code

Here's what my code block looks like:

php if (is_user_logged_in()) {
    $current_user = wp_get_current_user(); ?>

     // Update the visitor's full name with their WordPress display name.
     olark('api.visitor.updateFullName', {
             fullName: "<?php echo htmlspecialchars(
             $current_user->display_name,ENT_QUOTES); ?>"
      });

     // Update the visitor's email address with their WordPress account email.
        olark('api.visitor.updateEmailAddress', {
                emailAddress: "<?php echo htmlspecialchars(
                $current_user->user_email, ENT_QUOTES); ?>"
        });

<?php // Get the customer in Easy Digital Downloads.
        if (class_exists('EDD_Customer')) {
             $oCustomer = new EDD_Customer(
             $current_user->user_email);
             $nID = $oCustomer->id;
               if ($nID != 0) {
                   $sCustomerURL = admin_url(
                   'edit.php?post_type=download&page=edd-customers&view=overview&id=' 
                   . urlencode($nID)); ?>

       // Sends the operator a link to the customer's profile page.
               olark('api.chat.onBeginConversation', function () {

       // Notifies the operator - the visitor does not see this
               olark('api.chat.sendNotificationToOperator', {
                    body: "Customer Page: <?php echo $sCustomerURL; ?>"
                                });
                        });
                <?php }
        }
} ?>

(This box above scrolls to the right. OR You can also view this in Pastebin by going here.)

Let's break it down...

All of my code is wrapped in one single check, which is this first line:

php if (is_user_logged_in()) {

This makes all of my API code only trigger if the current visitor is logged into WordPress. There's no point trying to pass through the account details if they're not logged in!

After that, I use a WordPress function to get information about the current user:

$current_user = wp_get_current_user();

This fills the $current_user variable with the WP_User object. This contains useful details like the user's login name, email address, display name, and other information.

From there, I use the details from the WP_User object to get the current user's display name and email address, and populate some Olark fields with those values. The WordPress display name is retrieved using

php echo htmlspecialchars($current_user->display_name, ENT_QUOTES); ?>

and the email is retrieved using

php echo htmlspecialchars($current_user->user_email, ENT_QUOTES); ?>

Next, I have a section of code that's specifically related to the Easy Digital Downloads plugin. This includes one API call to send the agent a message when the conversation begins. The message contains a link to the customer's information page inside WordPress. This is a super easy way to look up the customer's entire profile and payment history.

This piece of code works by setting up a new EDD_Customer object and passing in the current user's email address:

$oCustomer = new EDD_Customer($current_user->user_email);

Once we have the object, we can get the customer ID number, which is the one piece of data we really need to construct the URL. We put together the URL using this line here:

$sCustomerURL = admin_url(
'edit.php?post_type=download&page=edd-customers&view=overview&id=' . urlencode($nID));

That gives us a link to the admin panel -- more specifically the customer's overview page in the admin panel.

Once we have all the info, we can put together the API request with Olark:

// Sends the operator a link to the customer's profile page.
olark('api.chat.onBeginConversation', function () {
    // Notifies the operator - the visitor does not see this
    olark('api.chat.sendNotificationToOperator', {
        body: "Customer Page: "
    });
});

This message is only sent to the agent when a conversation begins. The visitor doesn't see it, so it's totally private!

An image of two people telling a secret. Even when using the API to gather information about a customer, that information is kept private between the agent and the customer.Photo by London Scout on Unsplash

Did it work?

Absolutely! It pretty much did exactly what I expected and hoped it would, which is fantastic.

It has certainly increased efficiency. I don't have to ask my customers who they are before answering their questions, which cuts out a step for both of us. I can immediately see which customers are browsing the site, and if one of them submits a support ticket, I know exactly which visitor that is in Olark and I can initiate a live chat with them instead of replying to the support ticket. 

Want a chat widget on your Wordpress site?

Check out relevant topics on: Olark Essentials

Ashley Evans

Read more posts by Ashley Evans

Ashley is the founder of Nose Graze. An American girl hiding in England, she wants to help "Rock the hell out of your blog."