@flowkey/tracking-pipeline
v1.6.9
Published
Declarative pipeline for tracking events
Downloads
285
Keywords
Readme
Flowkey Tracking Pipeline
This is a library which helps implementing tracking events and user property changes of an app to multiple tracking services in a declarative and fully typed manner.
It does so by providing a trackEvent
function which consumes the app events and can be called from anywhere in the app. Then the different tracking integrations need to implement an eventTransformer
which converts the app events into a format that is usable for the tracking services.
The event transformer should be a pure function which has no side effects and does not access any global state or objects (e.g. window, etc.). For each app event it can return one event or multiple events as an array.
To actually transfer the service native events to the service the tracking integration also needs to implement a sendEvent
event function which is able to process one tracking service event.
In addition to that a global getCommonEventInfos
can be defined to gather global parameters which might be needed for every event (e.g. the userId, ip address, etc.)
Here is an example:
Tracking events
Define the events our app can send
type AppEvent =
| {
eventName: "SOMETHING_HAPPENED";
foo: string;
test: number;
}
| {
eventName: "I_NEED_TO_GENERATE_TWO_EVENTS";
};
Define a structure which our service can handle
type MyFancyTrackingServiceEvent = {
name: string;
data?: {
foo: string;
nested: {
test: number;
};
};
};
Implement getCommonEventInfos
// We want to send the userId with every event so let's grab it from our app service
const getCommonEventInfos = async () => ({
userId: await AuthService.getUserId()
});
// This automatically derives a type from the async function definition above
type CommonEventInfo = Parameters<
Parameters<ReturnType<typeof getCommonEventInfos>["then"]>[0]
>[0];
Implement our tracking integration
const myFancyServiceIntegration: EventTrackingIntegration<
AppEvent,
MyFancyTrackingServiceEvent,
CommonEventInfo
> = {
init: async () => {
// If needed we can initialize the service e.g. with a token
await myFancyService.init("MYTOKEN");
},
name: "test",
sendEvent: async (event, commonInfos) => {
// Here we take the common infos we want and the event and send itthem to our service
await myFancyService.sendEvent({
user: commonInfos.userId,
event: {
name: event.name,
data: event.data || {}
}
});
},
transformEvent: event => {
switch (event.eventName) {
case "SOMETHING_HAPPENED":
return {
// Our service uses different event names, so we map this here
name: "it happened",
data: {
foo: event.foo,
nested: {
test: event.test
}
}
};
case "I_NEED_TO_GENERATE_TWO_EVENTS":
return [
{
name: "it happened once"
},
{
name: "it happened twice"
}
];
default:
return [];
}
}
};
Create and initialize our app tracking functions
// Add as many integrations as you need
const integrations = [myFancyServiceIntegration];
const sendEvent = createSendEventFunction<AppEvent, any, CommonEventInfo>({
integrations,
// This turns on event logging
options: { debug: true }
});
const trackEvent = createTrackEventFunction({
sendEvent,
getCommonEventInfos
});
const initializeIntegrations = createInitializeIntegrationsFunction({
integrations
});
// Run the init calls of all integrations
await initializeIntegrations();
Send event from the app
await trackEvent({
eventName: "SOMETHING_HAPPENED",
foo: "bar",
test: 123
});
await trackEvent({
eventName: "I_NEED_TO_GENERATE_TWO_EVENTS"
});
User Properties
Tracking user property changes works similarly.
Create app user properties type
type UserProperties = {
name?: string;
email?: string;
lastLogin?: Date;
};
// This service is only interested in updating the last login as a unix timestamp
type FancyServiceUserProperties = {
lastLoginTimestamp?: string;
};
Create the integration
const myFancyServiceIntegration: UserPropertiesTrackingIntegration<
UserProperties,
FancyServiceUserProperties
> = {
transformUserProperties: userProperties =>
Object.fromEntries(
Object.entries(userProperties)
.map(([key, value]) => {
switch (key) {
case "lastLogin":
return ["lastLoginTimestamp", value.getTime()];
default:
return [];
}
})
.filter(entry => entry.length)
),
async updateUserProperties(
userId: string,
userProperties: FancyServiceUserProperties
) {
await myFancyService.updateUserProperties(userId, userProperties);
}
};
Wire everything together
const integrations = [myFancyServiceIntegration];
const updateUserProperties = createUpdateUserPropertiesFunction<
UserProperties,
FancyServiceUserProperties
>({
integrations
});
const trackUserProperties = createTrackUserPropertiesFunction({
updateUserProperties,
getUserId: () => AuthService.getUserId()
});
Send property updates from the app
await trackUserProperties({
name: "Foo Bar"
});
await trackUserProperties({
lastLogin: new Date()
});