# Having trouble using the hubspot tracking code API with google tag manager

**URL:** <https://community.hubspot.com/t/having-trouble-using-the-hubspot-tracking-code-api-with-google-tag-manager/54510>\
**Category:** APIs & Integrations\
**Tags:** starter\
**Created:** [April 6, 2022, 3:27pm UTC](https://community.hubspot.com/t/having-trouble-using-the-hubspot-tracking-code-api-with-google-tag-manager/54510 "2022-04-06T15:27:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MRobbins](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mrobbins/32/58551_2.png) [@MRobbins](https://community.hubspot.com/u/MRobbins)\
**Post date:** [April 6, 2022, 3:27pm UTC](https://community.hubspot.com/t/having-trouble-using-the-hubspot-tracking-code-api-with-google-tag-manager/54510/1 "2022-04-06T15:27:47Z")

</div>

Hi there, I have recently set up a code in google tag manger to store a cookie based of a query parameter. That cookie is then supposed to be pushed to a user after they submitted a form. The purpose is to figure out where a person came from to get to our site. I’ve set up the following code but still have no success. I’ve also tried placing an email manualy, but it still does not update the user. Please help!

```lia-code-sample
<script>

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

var urlParams;
(window.onpopstate = function () {
 var match,
 pl = /\+/g, // Regex for replacing addition symbol with a space
 search = /([^&=]+)=?([^&]*)/g,
 decode = function (s) { return decodeURIComponent(s.replace(pl, " ")); },
 query = window.location.search.substring(1);

 urlParams = {};
 while (match = search.exec(query))
 urlParams[decode(match[1])] = decode(match[2]);
})();

 var theref = urlParams["ref"];
 document.cookie = "ref="+theref+";max-age=7200";

window.addEventListener("message", function(event) {
 if (event.data.type === "hsFormCallback" &&
 event.data.eventName === "onFormSubmit" &&
 event.origin === document.location.origin) {

 setTimeout(function(){ 
 var _hsq = _hsq || [];
 _hsq.push(["identify",{
 usertoken: getCookie("hubspotutk"),
 referrer: getCookie("ref")
 }]);
 }, 10000);
 }
});

</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:** [April 7, 2022, 8:31pm UTC](https://community.hubspot.com/t/having-trouble-using-the-hubspot-tracking-code-api-with-google-tag-manager/54510/2 "2022-04-07T20:31:17Z")

</div>

@piersg , have any thoughts on this?

---

<div class="post-metadata">

**Author:** ![mangelet](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/mangelet/32/83031_2.png) [@mangelet](https://community.hubspot.com/u/mangelet)\
**Post date:** [May 19, 2022, 7:41am UTC](https://community.hubspot.com/t/having-trouble-using-the-hubspot-tracking-code-api-with-google-tag-manager/54510/3 "2022-05-19T07:41:19Z")

</div>

@MRobbins consider calling out a \_hsq.push([‘trackPageView’]); after the identify method.

\> The identify function only sets the identities in the tracker. The identities do not get passed to HubSpot until you make a separate trackPageView or trackEvent call like `_hsq.push([‘trackPageView’])`.
