简体   繁体   中英

Segment.io's Analytics.js Track: Pass an Object?

Is it possible to pass an object to the .track() method of analytics.js ?

analytics.track('button clicked', {
  prop1: 'val1',
  // object e.g. {prop2: 'val2', prop3: 'val3'}
});

I looked through their documentations and source code , but couldn't find an answer. I would greatly appreciate your help if you've had experience with this and have an answer.

Yes, you can pass a dictionary of properties for the track call. Here's an example:

analytics.track({
  event: 'Product Added',
  properties: {
    name: 'Ron Livingston',
    industry: 'Technology',
    value: 5
  }
});

If you want to know if you can call track like this...

analytics.track('event', { 
 prop1: 'blah', 
 objProp: { 
   thing1: 'yes', 
   thing2: 'no' 
}

...the answer is yes. However, it won't show up nicely in all of the integrations because the end tools don't accept object properties. The only one that I'm sure it will work with is Amplitude because they'll flatten the objects into dot notation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM