Skip to content

Overriding Timestamps

By default, when Tracking with Maps or using the Analytic/NamedAnalytic interfaces, the timestamp of the event is automatically set to the time at which the Hoglin#track call is made. But, in some situations, you may want to manually override this and set your own timestamp for an event. Let’s explore how to do this.


When tracking with maps or the interfaces, both will dumb down to a RecordedAnalytic under the hood. This is a class containing the event name, properties, and notably a timestamp. Typically, the timestamp is defaulted to the current time upon construction. However, we expose this class, along with a method for tracking with it: Hoglin#track(RecordedAnalytic<?> analytic), providing a way for you to set your own timestamp.


// Tracking with maps
Hoglin hoglin = /** Your Hoglin instance */;
final RecordedAnalytic<Map<String, Object>> analytic = new RecordedAnalytic<>(
"example_event",
Instant.now().minus(10, ChronoUnit.MINUTES),
Map.of(
"example_string", "test",
"example_integer", 123
)
);
hoglin.track(analytic);
// Using the Analytic/NamedAnalytic interface
class ExampleAnalytic implements NamedAnalytic {
final String exampleString;
final int exampleInteger;
ExampleAnalytic(final String exampleString, final int exampleInteger) {
this.exampleString = exampleString;
this.exampleInteger = exampleInteger;
}
@Override
public @NotNull String getEventType() { return "example_event"; }
}
Hoglin hoglin = /** Your Hoglin instance */;
final RecordedAnalytic<ExampleAnalytic> analytic = new RecordedAnalytic<>(
"example_event",
Instant.now().minus(10, ChronoUnit.MINUTES),
new ExampleAnalytic("test", 123)
);
hoglin.track(analytic);

This will track the example_event with a timestamp of 10 minutes ago.