Set user_id as page_view event

Hi all!

I am trying to set our hashed customerID as the user_id following these instructions.

I manage to get the user_id filled but it is a struct event. How do I get it to a page_view event?
This is the tracker:

window.snowplow('newTracker', 'cf', '{{SnowPlow Lookup Table}}', {
    contexts: {
    webPage: true,
    performanceTiming: true,
    gaCookies: true,
    geolocation: false
    }
  });
  
window.snowplow('trackPageView')

    window.snowplow('trackStructEvent', 
       'CustomerId',
       'Pageview',
       '{{CustomerId}}' 
    );

Thanks!

Hi Willem,

You’ve linked to documentation for a method you’re not using - you’re getting a structured event because you’re using the trackStructEvent method. You just need to use the setUserId method, from the documentation you’ve linked.

Hi @Colm,
thanks for your reply!

We tried two more things:

    contexts: {
    webPage: true,
    performanceTiming: true,
    gaCookies: true,
    geolocation: false
    }
  });
  
window.snowplow('trackPageView')
  
window.snowplow('setUserId', '{{CustomerId}}');

and 

window.snowplow('newTracker', 'cf', '{{SnowPlow Lookup Table}}', {
    contexts: {
    webPage: true,
    performanceTiming: true,
    gaCookies: true,
    geolocation: false
    }
  });
  
window.snowplow('trackPageView', null, [{
    schema: "iglu:com.example_company/page/jsonschema/1-2-1",
    data: {
        setUserid: '{{CustomerId}}',
    }
    }]
  );

It worked for getting the user_id field in the events table filled in with our customer_id, but they where still struct events and no page_views. Do you have an idea to get this added to the page_view?

The first way is best - just set the userid before you send the event.

The second way would also work if you had uploaded a schema to send it as a custom context but the first option is much simpler.

To explain a little more what’s going on here - the setUserid() method will assign a userid to a persistent object. Once it’s set all subsequent events will have the userid field set.

In general, if it’s a userid that you always have available, it’s best to set it right after tracker initialisation. If it’s a userid you’ll only have when a user signs in, it’s best to assign it at the point of signing in. You can choose to unassign it (by setting it to an empty string or null) when they sign out or not depending on your use case.

1 Like