Update `lifecycle` property within Events API


#1

Hey there Hubspotters,

I'm trying to help our Sales team get more value out of the Hubspot Platform, notably the Marketing Contacts. Currently, we have a register modal on our website to sign up a user in the Contacts tab, fired from the Events API. This works.

Once a User registers, our platform allows the opportunity to create and host their own events — a user can fill out a form with their event details and we will produce a public Event Details Page that the host can share. People can visit the Event Details Page and purchase tickets to said event. We have a separate event that tracks when the host actually posts their event to the public, called Published Event. This also works.

I'm trying to understand how to update a user's lifecycle property — the logic to transition them from a Subscriber to Customer should consist of the user publishing an event. I presume I'll need to execute the update code in the form where events are published...like something within the Published Event event that checks if this is a user's first event, and if so update their lifecycle property to customer.

Is this possible? Any help?

_register_modal.html.haml

document.querySelector("#new_user").onsubmit = ->
        fullName = document.querySelector('#full-name').value
        email = document.querySelector('#email').value
        firstName = fullName.split(" ")[0]
        _hsq = window._hsq = window._hsq || []

if fullName.indexOf(" ") != -1
      lastName = fullName.substring(fullName.indexOf(" "))
    else
      lastName = ""

_hsq.push(["identify",{
   email: email,
   firstName: firstName,
   lastName: lastName
}])

_hsq.push(["trackEvent", { id: 'sign up' }])

Listing-create-edit.coffee

_publishListing: ->
    @get('listing').save().then (listing) =>
      @set('listing.user_id', undefined)
      if "@@ENV.environment" == 'production'
        fbq 'trackCustom', 'CreatedListing', created_listing: 'createdListing'
      listing.submit().then () =>
        if "@@ENV.environment" != 'test'
          location.href = "/events/#{@get('listing.slug_param')}/manage"
        @get('flash').success @get('listingSuccessfullyCreated')
        @gaSuccess @get 'listing'
        analytics.track "Published Event", { Location: "create_form" }
        _hsq.push(["trackEvent", {
          id: "Published Event"
        }])
      , =>
        @get('flash').error 'Could not submit listing'
    , =>
      @get('flash').error @get('problemCreatingListing')

#2

Hi @tbcooney,

You can't directly update contact properties using the Events Javascript API, but there are a number of ways to update a contact's Lifecycle Stage when the Published Event event occurs. The identify method is the only Tracking Code API method that can update contact properties; you might consider making another identify call when a user publishes a page, including the Lifecycle Stage property like this:

_hsq.push(["identify",{
   email: email,
  lifecyclestage: "customer"
}])

The simplest method would actually be to create a simple workflow that enrolls contacts when they complete the Published Event event and updates their Lifecycle Stage to 'Customer'. This would eliminate the need to change anything about your code.

Let me know if you have any other questions on this!


#3

Thanks for explaining both approaches in your response Derek, really helpful :+1:


#4

Hey @Derek_Gervais...I need a hand now that I'm trying to implement the identify call when a user publishes a page.

I've placed the identify call before the trackEvent call; the trackEvent call shows up in the activity log but the Contact doesn't change to a "customer". Am I missing a comma to chain these two calls together or anything trivial?

_publishListing: ->
    @get('listing').save().then (listing) =>
      @set('listing.user_id', undefined)
      if "@@ENV.environment" == 'production'
        fbq 'trackCustom', 'CreatedListing', created_listing: 'createdListing'
      listing.submit().then () =>
        if "@@ENV.environment" != 'test'
          location.href = "/events/#{@get('listing.slug_param')}/manage"
        @get('flash').success @get('listingSuccessfullyCreated')
        @gaSuccess @get 'listing'
        analytics.track "Published Event", { Location: "create_form" }
        _hsq.push(["identify",{
          email: email,
          lifecyclestage: "customer"
        }])
        _hsq.push(["trackEvent", {
          id: "Published Event"
        }])
      , =>
        @get('flash').error 'Could not submit listing'
    , =>
      @get('flash').error @get('problemCreatingListing')

#5

Update - I replaced email: email with a hardcoded email of a Contact and it update the Lifecycle Stage when the event fired. I guess my issue is grabbing the users email; in prior identify calls, I grab the email from the signup form. Here, the user is already logged in, so I need to capture and pass the user.email


#6

Final implementation that I got working in coffeescript:

        email = @get('currentUser.email')

        _hsq.push(["identify",{
          email: email,
          lifecyclestage: "customer"
        }])