# Wordpress & HubSpot Visitor Identification API

**URL:** <https://community.hubspot.com/t/wordpress-hubspot-visitor-identification-api/30327>\
**Category:** APIs & Integrations\
**Tags:** developer-tools, sdk\
**Created:** [January 18, 2021, 12:58am UTC](https://community.hubspot.com/t/wordpress-hubspot-visitor-identification-api/30327 "2021-01-18T00:58:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jorge-gbs](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jorge-gbs/32/30597_2.png) [@jorge-gbs](https://community.hubspot.com/u/jorge-gbs)\
**Post date:** [January 18, 2021, 12:58am UTC](https://community.hubspot.com/t/wordpress-hubspot-visitor-identification-api/30327/1 "2021-01-18T00:58:37Z")

</div>

Hello,

I am trying to add the Vistior Identification API to my wordpress site that uses the Hubspot Wordpress Plugin.

I was adding a custom WP Plugin to handle this small addon. I think I am close to getting it working as I am able to retrive the token on the server and pass it to the client in JS.

Where I seem to be getting stuck on is getting the Chat to accept the loadImmediately False to delay the loading of the chat until I receive the token.

I think my issue is a timing issue when Wordpress loads the scripts but maybe I am wrong. Here is the JS that should be loading initially to stop the chat from loading. I have tried loading this from through WP Actions on init, wp\_enqueue\_scripts,wp\_head, and wp\_footer.

Any idea what I am missing?

```lia-code-sample
function onConversationsAPIReady() {
 console.log(`HubSpot Conversations API: ${window.HubSpotConversations}`);
	window.hsConversationsSettings = {
 loadImmediately: false,
 };
	console.log(window.hsConversationsSettings.loadImmediately);
 }

 /*
 If external API methods are already available, use them.
 */
 if (window.HubSpotConversations) {
 onConversationsAPIReady();
 } else {
 /*
 Otherwise, callbacks can be added to the hsConversationsOnReady on the window object.
 These callbacks will be called once the external API has been initialized.
 */
 window.hsConversationsOnReady = [onConversationsAPIReady];
 }

	console.log("Include Stop Load");

```

```lia-code-sample
	console.log('<?php echo $response ?>');
	console.log('<?php echo $current_user->user_email?>');
	window.hsConversationsSettings = {
		identificationEmail: '<?php echo $current_user->user_email?>',
		identificationToken: '<?php echo $response ?>'
	};
	window.hsConversationsOnReady = [
 () => {
 console.log('OnReady');
					window.HubSpotConversations.widget.load();
 },
 ];

```

```lia-code-sample
add_action( 'wp_head', 'Hubspot_Chat_Stop_Load');
add_action( 'wp_footer', 'Hubspot_Chat_User_Verify');

function Hubspot_Chat_User_Verify () { 

$current_user = wp_get_current_user(); 
if ( $current_user->user_email != "" ) {
	$apiKey = "<<API KEY>>"; //API Key from Hubspot

	$email = $current_user->user_email;
	$fn = $current_user->user_firstname;
	$ln = $current_user->user_lastname;

	/* https://packagist.org/packages/hubspot/api-client */
	$curl = curl_init();

	curl_setopt_array($curl, array(
		CURLOPT_URL => "https://api.hubapi.com/conversations/v3/visitor-identification/tokens/create?hapikey=".$apiKey,
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_ENCODING => "",
		CURLOPT_MAXREDIRS => 10,
		CURLOPT_TIMEOUT => 30,
		CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
		CURLOPT_CUSTOMREQUEST => "POST",
		CURLOPT_POSTFIELDS => "{\"email\":\"".$email."\",\"firstName\":\"".$fn."\",\"lastName\":\"".$ln."\"}",
		CURLOPT_HTTPHEADER => array(
			"accept: application/json",
			"content-type: application/json"
		),
	));

	$response = curl_exec($curl);
	$err = curl_error($curl);

	curl_close($curl);

	include(plugin_dir_path( __DIR__ ) . "hubspot-chat-user-verify/include.php");

}
}

function Hubspot_Chat_Stop_Load () {
	include(plugin_dir_path( __DIR__ ) . "hubspot-chat-user-verify/stopLoad.php");

}

```

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [January 19, 2021, 5:11pm UTC](https://community.hubspot.com/t/wordpress-hubspot-visitor-identification-api/30327/2 "2021-01-19T17:11:50Z")

</div>

Hey @jorge-gbs

Are you still working through this?

@prosa do you have any thoughts on this?

---

<div class="post-metadata">

**Author:** ![jorge-gbs](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jorge-gbs/32/30597_2.png) [@jorge-gbs](https://community.hubspot.com/u/jorge-gbs)\
**Post date:** [January 19, 2021, 7:23pm UTC](https://community.hubspot.com/t/wordpress-hubspot-visitor-identification-api/30327/3 "2021-01-19T19:23:49Z")

</div>

Hello, Yes I still am trying to figure it out.

---

<div class="post-metadata">

**Author:** ![jorge-gbs](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jorge-gbs/32/30597_2.png) [@jorge-gbs](https://community.hubspot.com/u/jorge-gbs)\
**Post date:** [January 27, 2021, 2:15am UTC](https://community.hubspot.com/t/wordpress-hubspot-visitor-identification-api/30327/4 "2021-01-27T02:15:56Z")

</div>

@dennisedson Any help on this would be greatly appriciated. I wouldn’t mind publishing this on Github for public use as it seems like a useful piece of code for anyone running wordpress and Hubspot. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [January 27, 2021, 2:24am UTC](https://community.hubspot.com/t/wordpress-hubspot-visitor-identification-api/30327/5 "2021-01-27T02:24:56Z")

</div>

Ah @Jorge-gb  
Sorry I didn’t see this still out there. I will look more closely tomorrow.  
[@Mike\_Eastwood](https://community.hubspot.com/t5/user/viewprofilepage/user-id/9259) is a real pro an might have an idea as well, though!

---

<div class="post-metadata">

**Author:** ![Den](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/den/32/73373_2.png) [@Den](https://community.hubspot.com/u/Den)\
**Post date:** [February 15, 2021, 5:13am UTC](https://community.hubspot.com/t/wordpress-hubspot-visitor-identification-api/30327/6 "2021-02-15T05:13:49Z")

</div>

It’s dissapointing that support of this API is not even planned for implementation in HubSpot WP plugin (we enquired).

@dennisedson can you please get HubSpot devs to provide working snippets and how-to for implementing this API in the presence of HubSpot WP plugin?

---

<div class="post-metadata">

**Author:** ![jorge-gbs](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jorge-gbs/32/30597_2.png) [@jorge-gbs](https://community.hubspot.com/u/jorge-gbs)\
**Post date:** [March 31, 2022, 7:54pm UTC](https://community.hubspot.com/t/wordpress-hubspot-visitor-identification-api/30327/7 "2022-03-31T19:54:14Z")

</div>

In case this helps anyone. I figured it out.

My code was correct. Just missing in the JS the Token I was passing the entire array including Keys instead of just the token.

The line read:

```lia-code-sample
	console.log('<?php echo $response ?>');
	console.log('<?php echo $current_user->user_email?>');
	window.hsConversationsSettings = {
		identificationEmail: '<?php echo $current_user->user_email?>',
		identificationToken: '<?php echo $response ?>'
	};
	window.hsConversationsOnReady = [
 () => {
 console.log('OnReady');
					window.HubSpotConversations.widget.load();
 },
 ];

```

It should read:

```lia-code-sample
<script type="text/javascript" id="passUserID">
	window.hsConversationsSettings = {
		loadImmediately: false,
		identificationEmail: '<?php echo $current_user->user_email?>',
		identificationToken: '<?php echo $response['token'] ?>'
	};
	window.hsConversationsOnReady = [
 () => {
 console.log('OnReady');
					window.HubSpotConversations.widget.load();
 },
 ];

</script>

```

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [March 31, 2022, 8:33pm UTC](https://community.hubspot.com/t/wordpress-hubspot-visitor-identification-api/30327/8 "2022-03-31T20:33:11Z")

</div>

Thank you so much for posting this!
