APIs & Integrations

MRobbins
Member

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

SOLVE

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!

 

 

<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>

 

0 Upvotes
1 Accepted solution
mangelet
Solution
Guide | Platinum Partner
Guide | Platinum Partner

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

SOLVE

@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'])`.

Martin Angeletti
HubSpot Veteran (12+ years)

Worried about messing up your HubSpot? I've got your back.

Join the thousands of people who have discovered how to avoid problems with simple tricks and have started to dominate HubSpot (and not be dominated).

️ Don't get left behind.

→ Click the subscribe button and scroll down to find the opt-in box.

Subscribe

Did I help answer your question? Mark this as a solution.

View solution in original post

2 Replies 2
mangelet
Solution
Guide | Platinum Partner
Guide | Platinum Partner

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

SOLVE

@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'])`.

Martin Angeletti
HubSpot Veteran (12+ years)

Worried about messing up your HubSpot? I've got your back.

Join the thousands of people who have discovered how to avoid problems with simple tricks and have started to dominate HubSpot (and not be dominated).

️ Don't get left behind.

→ Click the subscribe button and scroll down to find the opt-in box.

Subscribe

Did I help answer your question? Mark this as a solution.

dennisedson
HubSpot Product Team
HubSpot Product Team

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

SOLVE

@piersg , have any thoughts on this?

0 Upvotes