Nadia Conroy
Nadia Conroy

Reputation: 9

adding JSON as property

I have a JSON object that looks like this:

{
    "field": "value",
    "field2": "value",
    "field3": "value"
}

How can I add this to a keen event as a property similar to the "keen" object so I can reference individual fields, ie. my_property.field1

Upvotes: 0

Views: 47

Answers (1)

Michelle Wetzler
Michelle Wetzler

Reputation: 734

The properties on your events in Keen are based on whatever JSON you send in when you first post your event. You can post historical events, but you can't add new properties to events you've already posted. Here's an example of sending an event in JavaScript. Say your event is a tweet.

    var client = new Keen({
      projectId: 'PROJECT_ID',
      writeKey: 'WRITE_KEY'
    });
    
    var tweet_event = { 
      keen: {
        timestamp: new Date().toISOString(), // time the tweet happened
      },
      field: "value", // values you mentioned
      field2: "value",
      field3: "value,
      tweet: { // other properties you might have
        author: "@michellewetzler",
        text: "Dwell on the beauty of life. Watch the stars, and see yourself running with them. (Marcus Aurelius)"
      }
    }
    
    // Record the event (send it to Keen IO)
    client.recordEvent('tweets', tweet_event, function(err, res){ // name your collection here
      if (err) {
        document.getElementById('yeah').innerHTML = "Something is amiss. Check console for errors. Did you forget a comma perhaps? Or a curly brace?"
      }
      else {
        document.getElementById('yeah').innerHTML = "You just sent an event to Keen IO!"
      }
    });

then you can reference these properties in queries like:

var client = new Keen({
  projectId: "PROJECT_ID",
  readKey: "READ_KEY"
});

// count the number of tweets where field = value
var count = new Keen.Query("count", {
  event_collection: "tweets",
  timeframe: "this_14_days",
  filters: [
    {
      property_name: "field",
      operator: "eq",
      property_value: value
    }
  ]
});

// Send query
client.run(count, function(err, response){
  // if (err) handle the error
  console.log('result is: ', response.result);
});

Upvotes: 1

Related Questions