How to pass WordPress logged in user data to Continually

The ability to identify logged in users can be crucial for some businesses and websites. Continually allows you to achieve that by passing the lead's information in an identity in the embed code. We are using WordPress here as an example, but  same logic should work for any type of sites.

In this article we will cover:

In this article

Preparing the code

The first step is to customize the code responsible for passing the identity, we will pass the email, first name, last name, user name, and a static custom field called "logged_in" we will use it to build the bot's logic. We will use a wordpress function to check IF the user is logged, we will pass the data, ELSE we will display the normal embed code without any data in it.

<!-- Continually Start Code-->
<?php
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
echo '<script>var continuallySettings = { appID: "xxxxxxxx", identity: {email: "'. $current_user->user_email . '", name: "' . $current_user->user_firstname . ' ' . $current_user->user_lastname .'", wp_user_name: "' . $current_user->user_login . '", wp_logged_in: "Yes" }};</script>';} 
else {
    echo '<script>var continuallySettings = { appID: "xxxxxxxx" };</script>';}
?>
<script src="https://cdn-app.continual.ly/js/embed/continually-embed.latest.min.js"></script>
<!-- Continually End Code-->

First of all, Replace xxxxxxxx with the appID found in your embed code. be sure to replace it in the 5th and 7h row.

Let's explain the code:
1. & 10. is a comment tag to mark the start and end of the code.
2. & 8. is the start and end of the php code.
3 - 6 This is used to check if the user is logged in, execute the code in it and if the user is not logged in execute the code in the else function in rows 7-8
4. This is a WordPress function to get the logged-in user data.
5. This is the echo function used to output the code, learn more about it. We need to pass an identity in the embed code.

We will output the static part of the embed code between single quotes ' ', and when there is info we want to pass we will use one of the following default wordpress functions after we close the single quote, then we will start it again to continue the static part, like it is shown in the above example.

  • Username-> . $current_user->user_login .
  • User email-> . $current_user->user_email .
  • User first name-> . $current_user->user_firstname .
  • User last name-> . $current_user->user_lastname .
  • User display name-> . $current_user->display_name .
  • User ID-> . $current_user->ID .


Notice how we passed two custom fields  wp_user_name  which is an info we need to get from WP, that's why we used the  . $current_user->user_login . function. While the custom field wp_logged_in is a static one that we need to fill with "Yes" to run a conditional logic against it. 

In most cases, the above code is more than enough, you will get the important info to identify your logged in users, so you can just copy the code as it is unless you want to add more custom fields or remove some custom fields.

Note: We passed both WP first name and last name as a single fieldfor Continually, because Continually splits the name field automatically.

Where to insert the code

This code has PHP functions, it needs to be executed in the server side. That's why we need to disable the Continually plugin first if you have it installed. Then take a backup of your theme files before proceeding further.

1
Go to Appearance -> Theme Editor and make sure you are editing the active theme.
2
Click on the header.php file to start editing.
3
Paste the code just before closing </head> tag.
4
Click Update File, if the code is correct it will work as expected.

How to build your bot to differentiate between logged-in users

For this example, we will create a new bot from scratch, then add a conditional logic to check if the user is logged in or not.

1
We added two custom fields in our above code wp_logged_in & wp_user_name . So let's make them first :
How to create custom fields in Continually
2
Add a conditional logic object, choose the wp_logged_in custom field, and set the condition rule as  contains or Is -> Yes.
3
The true path will be the logged in path, start building your bot with different paths
4
Now let's test it!

How to identify logged in users for other CMS and websites

As long as you apply the same logic, you will be able to pass data to Continually from any Content Management System or custom websites, you need to know three things :

  1. The IF ELSE conditional function that can be used in that CMS.
  2. The function to get logged in user data and the function to output these data.

A quick search in Google can give you an answer for these functions and you can start building your code.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.