Using the identify function with HS tracking code and Google Tag Manager

SOLVE
Occasional Contributor

I'm looking to set-up the identify function so that we can track pageviews to specific users in the CRM, but wanted a little help in finding the right solution for our business.

 

A few things to know:

 

- We have the HubSpot Tracking Code installed on all pages via Google Tag Manager.

- Users login to an account on these pages, so we have the details of the person visiting the site - we now want to show this pageview on the activity record on the contact (Not all of our users have the cookie installed - this method will mean we get activity for every user)

- We want to use an ID property (our own user ID) to identify the user.

 

A couple of questions:

- Is it possible to simply add the identify function to the existing Tracking code in GTM? Or would this be a separate piece of code that needs to be installed on the pages we want? Do you have an example as to how this might look?

- When using our own user ID property to identify a user, what needs to change on the function? This is the example provided on the docs:

 

var _hsq = window._hsq = window._hsq || [];
_hsq.push(["identify",{
    email: user.email,
    id: user.id
}]);

Our ID property exists as a contact property in HS and is labelled as 'user_id'. Would the code change to something like this:

 

var _hsq = window._hsq = window._hsq || [];
_hsq.push(["identify",{
    email: user.email,
    user_id: user.id
}]);

I'm not a developer, but trying to get as much understanding as how to do this as possible as I'd like to get this functionality live as soon as possible.

 

Thanks,

Dan

 

Reply
0 Upvotes
1 Accepted solution

Accepted Solutions
Highlighted
Community Manager

Hey @d_greenhalgh,

 

This piece of code looks just fine! Just two things to note:

 

1. Do also ensure that the HubSpot Tracking Code is present on your page. 

2. You would need to include an email address to tie the data to a contact. As stated on this documentation - Identify a visitor:

 

However, unlike with an email address, including an 'id' by itself will not create a contact.

You may test it out by hardcoding an email and see if it creates a contact. Let me know how it goes!

Reply
0 Upvotes
4 Replies 4
Highlighted
Community Manager

Hey @d_greenhalgh,

 

Let's walk through the questions one by one:

1. Could you share a little more behind the reason in adding HubSpot identify function  with the GTM code? I would think that if the GTM is firing correctly, it shouldn't be an issue to include the following example code with the GTM code:

 

<!-- Google Tag Manager -->
<script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src='https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','myNewName','GTM-XXXX');

var _hsq = window._hsq = window._hsq || [];
_hsq.push(["identify",{
    email: getParameterByName("email"),
    favorite_color: 'orange'
}]);

</script>
<!-- End Google Tag Manager -->

 

2. Spot on! If you're looking to set custom property - user_id, you would need to change id to user_id. This is because the 'id' cannot be found on the contact record/use to search contact record, as stated on this documentation - Identify a visitor:

 

Also, this 'id' is treated as a completely external identity, so while analytics data can be associated with a specific contact record by the ID (if, for example, you've previously identified a record by ID and by email, or the record was previously identified by ID and the visitor also has a form submission), the contact record cannot be looked up by this ID.

Note: This external ID can only be used with the HubSpot tracking code. This ID cannot be used to retrieve or update any records through any other HubSpot tools or APIs.

Additionally, do note that the identify function only store data on the tracker. If you'd like to pass the data into HubSpot, you would need to call either the trackPageView or trackEvent functions. 

 

Hope this helps to shed some light and do let me know if there's anything else you'd like further clarification on!

Reply
0 Upvotes
Highlighted
Occasional Contributor

Thanks @WendyGoh!

 

1. We're looking to use GTM because that's how we have already implemented the hubspot tracking code. So was hoping that we could keep it all in the same tag. What would be the recommended way to implement the identify function?

 

I've just been trying to test using a test site. Do you think something like this would work: 

http://jonmilsom.htmlbin.net/hubspot-identify.html

 

<!DOCTYPE html>
<html>
    <head>
        <title>HubSpot Identify Test</title>
    </head>
    <body>
      <h1>HubSpot Identify Test</h1>
      <!-- Start of Async HubSpot Analytics Code -->
        <script type="text/javascript">
          (function(d,s,i,r) {
            if (d.getElementById(i)){return;}
            var n=d.createElement(s),e=d.getElementsByTagName(s)[0];
            n.id=i;n.src='//js.hs-analytics.net/analytics/'+(Math.ceil(new Date()/r)*r)+'/2547723.js';
            e.parentNode.insertBefore(n, e);
          })(document,"script","hs-analytics",300000);
          
          
          var _hsq = window._hsq = window._hsq || [];
          _hsq.push(["identify",{
              user_id: '143'
          }]);
          
          // Track a new page using setPath:
          // Update the path stored in the tracker:
          var _hsq = window._hsq = window._hsq || [];
          _hsq.push(['setPath', '/#/new-page']);
          // Track the page view for the new page
          _hsq.push(['trackPageView']);
        </script>
      <!-- End of Async HubSpot Analytics Code -->
      

      
    </body>
</html>

Thanks,

Dan

Reply
0 Upvotes
Highlighted
Community Manager

Hey @d_greenhalgh,

 

This piece of code looks just fine! Just two things to note:

 

1. Do also ensure that the HubSpot Tracking Code is present on your page. 

2. You would need to include an email address to tie the data to a contact. As stated on this documentation - Identify a visitor:

 

However, unlike with an email address, including an 'id' by itself will not create a contact.

You may test it out by hardcoding an email and see if it creates a contact. Let me know how it goes!

Reply
0 Upvotes
Highlighted
Occasional Contributor

Thanks @WendyGoh. Think we've got this working correctly now.

 

Cheers,
Dan

Reply
0 Upvotes