User tracking in Ruby on Rails site


#1

Hi All,

I have created a membership site using Ruby on Rails and when users sign up I create the contact in hubspot. I don't get any user tracking and wondered what i was missing?

As its not a hubspot form on signup do I need to write some custom javascript?

Thanks in advance.


Create a contact via API and associating with cookie based activity
#3

Welcome, @stevesizer!

Happy to help. Are you creating contacts with the Contacts API?

If so, the Contacts API is better suited for offline conversions and can't pass browsing information. If your leads are converting online, it's best to use either the Forms API or the tracking code's identify method.

If you are using the Forms API, are you scraping and passing the HubSpot utk cookie token value in the context object? If you believe you are and you're still not seeing tracking information, share links to a few example contacts and I'll take a look!


#4

Hi Isaac,

I'm creating the contacts via the Contacts API as I'm using the hubspot-ruby gem in my rails application.

I have tried to make use of the identify method, by checking to see if the user has signed into the application if so I then call the identify method as I will know the users email at that point? Does that sound ok?

Thanks,
Steve


#5

Hi @stevesizer,

Yep, that all makes sense.

All contacts created via the Contacts API will have the Original Source value "Offline Sources" and no page views at first. You can pass page view and source tracking information later with the identify method — assuming the contact is on the same browser and hasn't cleared their cookies — and this should update the Original Source property value.

While the flow above will work, if capturing page views and the correct Original Source value from the get-go is a priority for you, then I still recommend using the Forms API to pass the hutk cookie token value with the initial submission. I recognize that this would require more work up front, however.


#6

How does this look?

 <% if user_signed_in? %>
  <script type="text/javascript">
    $(document).ready( function() {
      var _hsq = window._hsq = window._hsq || [];
      _hsq.push(["identify",{
        email: <%= current_user.email %>
      }]);
      _hsq.push(['setPath', window.location.pathname]);
    // Track the page view for the new page
      _hsq.push(['trackPageView']);
    });
  </script>
  <% else %>
    <script type="text/javascript">
    $(document).ready( function() {
      var _hsq = window._hsq = window._hsq || [];
      _hsq.push(['setPath', window.location.pathname]);
    // Track the page view for the new page
      _hsq.push(['trackPageView']);
    });
  </script>
  <% end %>

I check to see if the user is logged in. If so we know the users email, set the identity and track page views. If user is not logged in we don't set the identity and hope they have the cookie and track page views. Would setting identify on every page cause a problem?

Thanks,
Steve


#7

Hi @stevesizer,

That code looks good to me.

Just keep in mind that — as this documentation notes — calling trackPageView "manually before or during the initial page load could lead to duplicate views being tracked." I don't know where exactly the code above lies on your site, so it may well be appropriate to trigger it manually.

I would not recommend calling identify on each page. When you identify a visitor and trackPageView runs (either manually or through the tracking code), HubSpot associates the cookie and page views with a contact's user token. The tracking code then remembers the cookie going forward, so you don't need to identify the visitor again unless they have a new cookie. Thus, I think your decision to include the identify function in your log in flow is best.


#8

Ok Isaac here is what I have done....

ive added this piece of code for when users login...

( "#new_user" ).submit(function( event ) { var _hsq = window._hsq = window._hsq || []; _hsq.push(["identify",{ email: ('#user_email').val();
}]);
_hsq.push(['setPath', window.location.pathname]);
// Track the page view for the new page
_hsq.push(['trackPageView']);
});

this runs on the login form submit. It identifies the user as we know the email address. I have also got the tracking code in the header of all pages.


#9

Hey, @stevesizer.

I ran some tests on my own site and confirmed that the identify will update a contact record's web history and Original Source from "Offline Sources," similar to what is described in this article. We're looking to be in good shape!

That said, I'm noticing a few things with your code. The identify method's documentation defines the email value in single quotes without a semicolon at the end. For example:

var _hsq = window._hsq = window._hsq || [];
_hsq.push(["identify",{
    email: 'email_to_identify@example.com'
}]);

This is the same format I used in my successful tests. Your code, however, reads:

var _hsq = window._hsq = window._hsq || [];
_hsq.push(["identify",{
    email: ('#user_email').val();

If that doesn't work, I would try removing the val(); and any extra parentheses.


#10

I get the following error. I using jQuery to capture the form submit and then call the identify method


#11

Hey, @stevesizer. Could you share a link to that page?


#12

https://membership.opexfit.com/users/sign_in


#13

Thanks, @stevesizer.

I haven't been able to replicate that error; the identify method is working perfectly for me.

I copied your form's HTML to this test page and modified the script slightly to be:

<script type="text/javascript">
  $( "#new_user" ).submit(function( event ) {
    var user_email = $('#user_email').val();
    var _hsq = window._hsq = window._hsq || [];
    _hsq.push(["identify",{
        email: user_email,
        firstname: "Test",
        lastname: "B1"
    }]);
    // Track the page view for the new page
    _hsq.push(['trackPageView']);
    alert(user_email);
  });
</script>

This screencast shows me entering the email itb1+foo@hubspot.com, which then pops up from the alert, so we know that email was defined.

Furthermore, a new contact was successfully created in my account:


#14

Connor tested the form on your site and it also successfully created this contact.


#15

Excellent so that should be working then :slight_smile: