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.