# hubspot cookie callback

**URL:** <https://community.hubspot.com/t/hubspot-cookie-callback/37039>\
**Category:** CMS Development\
**Created:** [May 10, 2021, 6:22pm UTC](https://community.hubspot.com/t/hubspot-cookie-callback/37039 "2021-05-10T18:22:29Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FViana](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/fviana/32/82797_2.png) [@FViana](https://community.hubspot.com/u/FViana)\
**Post date:** [May 10, 2021, 6:22pm UTC](https://community.hubspot.com/t/hubspot-cookie-callback/37039/1 "2021-05-10T18:22:29Z")

</div>

Hey!

I need help, we have a website with the integration of the hubspot in the contact forms and in the widget of our bot.

However, our widget is loaded before the hubspot by the wordpress footer script. Is there any way to place a callback on the main hubspot script? so that our widget is loaded with the value of the hubspotutk variable?

We are currently trying to get the cookie like this, but our script must ‘wait’ for the hubspot, as the value does not exist, the variable is empty.

```lia-code-sample

function getCookieGeneric(cname) {
 var name = cname + "=";
 var decodedCookie = decodeURIComponent(document.cookie);
 var ca = decodedCookie.split(';');
 for (var i = 0; i < ca.length; i++) {
 var c = ca[i];
 while (c.charAt(0) == ' ') {
 c = c.substring(1);
 }
 if (c.indexOf(name) == 0) {
 return c.substring(name.length, c.length);
 }
 }
 return "";
}

var prechat_inputs = {};
prechat_inputs["hubspotutk"] = getCookieGeneric('hubspotutk');

var chatbotKey = 'someKEYHere';

(function(d, r, o, z, a, k, t) {
 a = d.getElementsByTagName('head')[0];
 k = d.createElement('script');
 k.async = 1;
 t = (Math.floor(Math.random() * 1000000) + 1);
 k.src=r + '/' + o + '/droz.js?i=' + z + '&u=' + r + '&v=' + t;
 a.appendChild(k);
 })(document, 'https://chat-app.meudroz.com', 'v1', chatbotKey);

```

---

<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:** [May 11, 2021, 1:56pm UTC](https://community.hubspot.com/t/hubspot-cookie-callback/37039/2 "2021-05-11T13:56:50Z")

</div>

@piersg , @Kevin-C how would you handle this?

---

<div class="post-metadata">

**Author:** ![FViana](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/fviana/32/82797_2.png) [@FViana](https://community.hubspot.com/u/FViana)\
**Post date:** [May 17, 2021, 3:51pm UTC](https://community.hubspot.com/t/hubspot-cookie-callback/37039/3 "2021-05-17T15:51:33Z")

</div>

Anyone can help me?

Thanks.

---

<div class="post-metadata">

**Author:** ![FViana](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/fviana/32/82797_2.png) [@FViana](https://community.hubspot.com/u/FViana)\
**Post date:** [May 17, 2021, 6:08pm UTC](https://community.hubspot.com/t/hubspot-cookie-callback/37039/4 "2021-05-17T18:08:46Z")

</div>

I found a solution, but I haven’t tested it yet, I believe there is an easier way but I haven’t found it anywhere.

It can be considered a workaround.

If you do not have the cookie (page was loaded for the first time) … run this:

```lia-code-sample
		var checkCookie = function() {

		var lastCookies = document.cookie.split( ';' ).map( function( x ) { return x.trim().split( /(=)/ ); } ).reduce( function( a, b ) { 
				a[b[ 0] ] = a[b[ 0] ] ? a[b[ 0] ] + ', ' + b.slice( 2 ).join( '' ) : 
				b.slice( 2 ).join( '' ); return a; }, {} );

		return function() {

			var currentCookies = document.cookie.split( ';' ).map( function( x ) { return x.trim().split( /(=)/ ); } ).reduce( function( a, b ) { 
				a[b[ 0] ] = a[b[ 0] ] ? a[b[ 0] ] + ', ' + b.slice( 2 ).join( '' ) : 
				b.slice( 2 ).join( '' ); return a; }, {} );

			for(cookie in currentCookies) {
				if ( currentCookies[cookie] != lastCookies[cookie] ) {
					console.log("--------")
					console.log(cookie+"="+lastCookies[cookie])
					console.log(cookie+"="+currentCookies[cookie])
 // get value from hubspotutk !!
 // call my widget
				}

			}
			lastCookies = currentCookies;

		};
		}();

		window.setInterval(checkCookie, 100);

```

if you have the cookie, verified by the getCookieGeneric function (which I posted up there) take the value and call our widget.

This is not ideal, more is something, if anyone has any better ideas please post here below

---

<div class="post-metadata">

**Author:** ![FViana](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/fviana/32/82797_2.png) [@FViana](https://community.hubspot.com/u/FViana)\
**Post date:** [May 19, 2021, 6:32pm UTC](https://community.hubspot.com/t/hubspot-cookie-callback/37039/5 "2021-05-19T18:32:57Z")

</div>

@dennisedsonis this a good solution? 🤔
