Getting started
The following steps will guide you through setting up the Rust tracker in your project and tracking a simple event.
Installation
Add the snowplow_tracker as a dependency in Cargo.toml inside your Rust application:
[dependencies]
snowplow_tracker = 0.2.0
Use the package APIs in your code:
use snowplow_tracker::Snowplow;
Initialization
Instantiate a tracker using the Snowplow::create_tracker
function. The function takes three required arguments: namespace
, app_id
, collector_url
, and one optional argument, subject
. Tracker namespace
identifies the tracker instance; you may create multiple trackers with different namespaces. The app_id
identifies your app. The collector_url
is the URI of the Snowplow collector to send the events to. subject
allows for an optional Subject to be attached to the tracker, which will be sent with all events.
let mut tracker = Snowplow::create_tracker("ns", "app_id", "https://...", None);
There are additional optional arguments to configure the tracker. To learn more about configuring how events are sent, check out this page.
Tracking events
To track events, simply instantiate their respective types and pass them to the tracker.track method with optional context entities. Please refer to the documentation for specification of event properties.
// Tracking a Screen View event
let screen_view_event = ScreenViewEvent::builder()
.id(Uuid::new_v4())
.name("a screen view")
.previous_name("previous name")
.build()?;
let screen_view_event_id = tracker.track(screen_view_event, None)?;
Safely closing the emitter
The emitter spawns its own thread to send events using a Tokio runtime. To allow the tracker to drop, the emitter must be closed. This can be done by calling Tracker.close_emitter()
.
let mut tracker = Snowplow::create_tracker("ns", "app_id", "https://...", None);
// track some events...
tracker.close_emitter();
Visit documentation about tracking events to learn about other supported event types. You may also want to read about adding more data to tracked events.
Logging
The log crate is used for logging. To enable logging, you must add a logger to your application, such as env_logger.
Testing
Testing that your event tracking is properly configured can be as important as testing the other aspects of your app. It confirms that you are generating the events you expect.
We recommend using Snowplow Micro for testing and debugging — you can even use it for automated tests.