Collecting data with Trackers and Webhooks

  1. Home
  2. Docs
  3. Collecting data with Trackers and Webhooks
  4. Trackers – collecting data from your own applications
  5. Javascript Tracker
  6. Advanced Usage
  7. Callbacks

Callbacks

If you call snowplow_name_here with a function as the argument, the function will be executed when sp.js loads:




snowplow_name_here(function () { console.log("sp.js has loaded"); });

Or equivalently:

snowplow_name_here(function (x) { console.log(x); }, "sp.js has loaded");

The callback you provide is executed as a method on the internal trackerDictionary object. This means that you can access the trackerDictionary using this:

// Configure a tracker instance named "cf" snowplow_name_here('newTracker', 'cf', 'd3rkrsqld9gmqf.cloudfront.net', { appId: 'snowplowExampleApp', platform: 'web' }); // Access the tracker instance inside a callback snowplow_name_here(function () { var cf = this.cf; var userFingerprint = cf.getUserFingerprint(); doSomethingWith(userFingerprint); })

The callback function should not be a method:

// TypeError: Illegal invocation snowplow_name_here(console.log, "sp.js has loaded");

will not work, because the value of this in the console.log function will be trackerDictionary rather than console.

You can get around this problem using Function.prototoype.bind as follows:

snowplow_name_here(console.log.bind(console), "sp.js has loaded");

For more on execution context in JavaScript, see the MDN page.