1

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.

Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
Steve
  • 4,946
  • 12
  • 45
  • 62

1 Answers1

2

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.

  • Thank you for your answer! So I'd do it like `properties: [object]`. I was wondering if I can include other properties alongside the object, any ideas on how to do that? – Steve Sep 25 '14 at 00:05
  • The docs specificall say to not nest your properties in a Segment track call: https://segment.com/academy/collecting-data/the-anatomy-of-a-track-call/ (DON’T send “nested” properties) – Connor Leech Jun 12 '18 at 20:41