APIs & Integrations


HubSpot integration with Wordpress login/logout

This may be more of a feature request than a question but:


I'm wondering if the HubSpot for Wordpress plugin can be made to hook into the native Wordpress logout/login functionality.


I'm trying to make sure that the chat info and HubSpot form data is not accidentally remembered if some users happen to use the same browser.  What I've done so far is add a Wordpress action to clear the relevant HubSpot cookies:

function clear_one_cookie( $cookie, $domain = "" ) {
	setcookie( $cookie, '', 1, '/', $domain );
function clear_hubspot_cookies_on_logout() {
	// Remove HubSpot tracking cookies, which are set across the top-level domain
	clear_one_cookie( '__hstc', '.example.com' );
	clear_one_cookie( 'hubspotutk', '.example.com' );
	// Remove HubSpot message tracking cookie, which uses the local (sub)domain
	clear_one_cookie( 'messagesUtk', '.site.example.com' );
add_action( 'wp_logout', 'clear_hubspot_cookies_on_logout' );

This has achieved the logout integration.


Now, once a user has logged in (which is not typically captured by HubSpot because it's just a username/password), I'm trying to figure out how to hook the page so that I can push the current user's email address to the HubSpot Tracking API: https://developers.hubspot.com/docs/methods/tracking_code_api/identify_visitor

And to do so before the page view is tracked.


Anyway, in this whole process, it strikes me that there's no reason why the HubSpot for Wordpress plugin shouldn't automatically support this type of integration.  I'd be happy if someone were able to point me to where this already exists 🙂

3 Replies 3

HubSpot integration with Wordpress login/logout

I was shocked to find that this even occurs!

Massive data protection and privacy risk! We've just had a website user wrongly assumed to be someone that has previously used the same browser. I've tried utilising your code, @eterobby , but unfortunately it's not made any difference


HubSpot integration with Wordpress login/logout

Ok, the following code appears to integrate with the Identify API:


add_action( 'wp_enqueue_scripts', function () {
	$current_user = wp_get_current_user();
	if ( !$current_user->exists() ) {
		return; // not logged in
	$current_user_email = $current_user->user_email;
	if ( empty($current_user_email) ) {
		return; // missing email

	// Create the JSON payload to push
	$hsdata = array('identify', array('email' => $current_user_email));

	// Push the data into the HubSpot queue before the HubSpot loader script tag
	$script = 'var _hsq = window._hsq = window._hsq || [];';
	$script .= '_hsq.push('.json_encode($hsdata).');';
	wp_add_inline_script('leadin-script-loader-js', $script, 'before');


I still think this functionality should be baked into the HubSpot plugin (at least as an option), but hopefully this post is sufficient for others to solve the problem in the interim.


By the way, I'm using the Wordpress Snippets plugin to add these snippets safely to my Wordpress instance.



HubSpot integration with Wordpress login/logout

This is a gem. I thank you very much! It seems like it's working fine, but I'll know for sure after a few tests. It's exactly what I was looking for and it could be a real boost for my saling efforts.


I agree with you that this should be build-in with the wordpress plugin. As you've shown it's not that difficult to implement. Or at least they can add this to their documentation.