# User tracking in Ruby on Rails site

**URL:** <https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754>\
**Category:** APIs & Integrations\
**Tags:** contacts\
**Created:** [November 22, 2018, 1:48am UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754 "2018-11-22T01:48:10Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [November 22, 2018, 1:48am UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/1 "2018-11-22T01:48:10Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [November 26, 2018, 10:59pm UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/2 "2018-11-26T22:59:51Z")

</div>

Welcome, [@stevesizer](https://community.hubspot.com/u/stevesizer)!

Happy to help. Are you creating contacts with the [Contacts API](https://developers.hubspot.com/docs/methods/contacts/create_contact)?

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](https://developers.hubspot.com/docs/methods/forms/submit_form_v3) or the tracking code’s [identify](https://developers.hubspot.com/docs/methods/tracking_code_api/identify_visitor) 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!

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [November 27, 2018, 8:54am UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/3 "2018-11-27T08:54:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [November 27, 2018, 1:29pm UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/4 "2018-11-27T13:29:57Z")

</div>

Hi [@stevesizer](https://community.hubspot.com/u/stevesizer),

Yep, that all makes sense.

All contacts created via the Contacts API will have the _Original Source_ value [“Offline Sources”](https://knowledge.hubspot.com/articles/kcs_article/reports/how-were-contacts-added-to-hubspot-from-an-offline-source) 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](https://knowledge.hubspot.com/articles/kcs_article/contacts/why-did-my-contacts-original-source-type-change).

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](https://developers.hubspot.com/docs/methods/forms/submit_form_v3) to pass the `hutk` cookie token value with the initial submission. I recognize that this would require more work up front, however.

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [November 27, 2018, 1:47pm UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/5 "2018-11-27T13:47:43Z")

</div>

How does this look?

```auto
 <% 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

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [November 27, 2018, 6:47pm UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/6 "2018-11-27T18:47:05Z")

</div>

Hi [@stevesizer](https://community.hubspot.com/u/stevesizer),

That code looks good to me.

Just keep in mind that — as [this documentation](https://developers.hubspot.com/docs/methods/tracking_code_api/track_page_view) 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.

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [November 29, 2018, 1:08pm UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/7 "2018-11-29T13:08:54Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [November 29, 2018, 6:56pm UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/8 "2018-11-29T18:56:04Z")

</div>

Hey, [@stevesizer](https://community.hubspot.com/u/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](https://knowledge.hubspot.com/articles/kcs_article/contacts/why-did-my-contacts-original-source-type-change). We’re looking to be in good shape!

[

 ![](https://d2qpr2jzcazej7.cloudfront.net/original/3X/9/4/9498f0d42f30baca48ff4e6d084946cce9f71224.jpeg)

Identify method works.jpg1122x689 107 KB

]([https://community.hubspot.com/legacyfs/online/uploads/default/original/2X/9/98f823be6a43a166cbc2cdda98e928975bcf46c4.jpeg](https://community.hubspot.com/legacyfs/online/uploads/default/original/2X/9/98f823be6a43a166cbc2cdda98e928975bcf46c4.jpeg))

That said, I’m noticing a few things with your code. [The `identify` method’s documentation](https://developers.hubspot.com/docs/methods/tracking_code_api/identify_visitor) defines the `email` value in single quotes _without_ a semicolon at the end. For example:

```auto
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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [November 29, 2018, 7:25pm UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/9 "2018-11-29T19:25:59Z")

</div>

[

 ![](https://d2qpr2jzcazej7.cloudfront.net/original/3X/1/2/1258d5b93e41500d3ab350a3bf90f2cd1f4dcec7.png)

Screenshot 2018-11-29 at 19.11.50.png3347x1719 549 KB

]([https://community.hubspot.com/legacyfs/online/uploads/default/original/2X/4/4a5ef0bd53aac7cf8048dc200b776bd7de2b2430.png](https://community.hubspot.com/legacyfs/online/uploads/default/original/2X/4/4a5ef0bd53aac7cf8048dc200b776bd7de2b2430.png))

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

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [November 29, 2018, 8:10pm UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/10 "2018-11-29T20:10:35Z")

</div>

Hey, [@stevesizer](https://community.hubspot.com/u/stevesizer). Could you share a link to that page?

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [November 29, 2018, 8:29pm UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/11 "2018-11-29T20:29:16Z")

</div>

[https://membership.opexfit.com/users/sign\_in](https://membership.opexfit.com/users/sign_in)

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [November 29, 2018, 9:13pm UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/12 "2018-11-29T21:13:54Z")

</div>

Thanks, [@stevesizer](https://community.hubspot.com/u/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](http://www.chimeraseniorsociety.com/tracking-code-api-test-page-2) and modified the script slightly to be:

```auto
<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](http://recordit.co/tZtQfURhNi) 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:

[

 ![](https://d2qpr2jzcazej7.cloudfront.net/original/3X/6/8/689c4cdf859045c7b7b3c10e2131122833210177.jpeg)

Contact created.jpg1600x844 117 KB

]([https://community.hubspot.com/legacyfs/online/uploads/default/original/2X/9/903ec6d81fdf52a9b0f1edaf111c1357964f5c93.jpeg](https://community.hubspot.com/legacyfs/online/uploads/default/original/2X/9/903ec6d81fdf52a9b0f1edaf111c1357964f5c93.jpeg))

---

<div class="post-metadata">

**Author:** ![IsaacTakushi](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/isaactakushi/32/70387_2.png) [@IsaacTakushi](https://community.hubspot.com/u/IsaacTakushi)\
**Post date:** [November 29, 2018, 9:51pm UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/13 "2018-11-29T21:51:00Z")

</div>

Connor tested the form on [your site](https://membership.opexfit.com/users/sign_in) and it also successfully created [this contact](https://app.hubspot.com/contacts/2810903/contact/11216051/?interaction=note).

---

<div class="post-metadata">

**Author:** ![system](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/system/32/124_2.png) [@system](https://community.hubspot.com/u/system)\
**Post date:** [November 29, 2018, 10:06pm UTC](https://community.hubspot.com/t/user-tracking-in-ruby-on-rails-site/11754/14 "2018-11-29T22:06:24Z")

</div>

Excellent so that should be working then  
 ![slight_smile.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/3/33fb2a14544119c2351fe774141779702299e6a6.png)
