APIs & Integrations

Not applicable

User tracking in Ruby on Rails site

SOLVE

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.

0 Upvotes
1 Accepted solution
IsaacTakushi
Solution
HubSpot Employee
HubSpot Employee

User tracking in Ruby on Rails site

SOLVE

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

Isaac Takushi

Associate Certification Manager

View solution in original post

0 Upvotes
13 Replies 13
Not applicable

User tracking in Ruby on Rails site

SOLVE

Excellent so that should be working then :slight_smile:

IsaacTakushi
Solution
HubSpot Employee
HubSpot Employee

User tracking in Ruby on Rails site

SOLVE

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

Isaac Takushi

Associate Certification Manager
0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

User tracking in Ruby on Rails site

SOLVE

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:

Isaac Takushi

Associate Certification Manager
0 Upvotes
Not applicable

User tracking in Ruby on Rails site

SOLVE
0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

User tracking in Ruby on Rails site

SOLVE

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

Isaac Takushi

Associate Certification Manager
0 Upvotes
Not applicable

User tracking in Ruby on Rails site

SOLVE

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

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

User tracking in Ruby on Rails site

SOLVE

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.

Isaac Takushi

Associate Certification Manager
0 Upvotes
Not applicable

User tracking in Ruby on Rails site

SOLVE

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.

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

User tracking in Ruby on Rails site

SOLVE

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.

Isaac Takushi

Associate Certification Manager
0 Upvotes
Not applicable

User tracking in Ruby on Rails site

SOLVE

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

0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

User tracking in Ruby on Rails site

SOLVE

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.

Isaac Takushi

Associate Certification Manager
0 Upvotes
IsaacTakushi
HubSpot Employee
HubSpot Employee

User tracking in Ruby on Rails site

SOLVE

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!

Isaac Takushi

Associate Certification Manager
0 Upvotes
Not applicable

User tracking in Ruby on Rails site

SOLVE

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

0 Upvotes