# @rogu/tracker-runtime

> This is a runtime library for [@rogu/tracker-codegen](). However, you still need to setup the provider manually to make it works.

Latest version **0.2.0** (published 2020-11-18) · MIT license · 0 weekly downloads

## Install

```sh
npm install @rogu/tracker-runtime
pnpm add @rogu/tracker-runtime
yarn add @rogu/tracker-runtime
bun add @rogu/tracker-runtime
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2020-11-18 |
| First published | 2020-10-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 47.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | broerjuang |
| Maintainers | broerjuang |

## Links

- npm: https://www.npmjs.com/package/@rogu/tracker-runtime
- npm.io page: https://npm.io/package/@rogu/tracker-runtime

## Recent versions

- 0.2.0 (latest) — 2020-11-18
- 0.1.0-alpha-0 — 2020-10-19
- 0.1.0-dev-1 — 2020-10-13
- 0.1.0 — 2020-10-13
- 0.0.1 — 2020-10-09

## README

# @rogu/tracker-runtime

This is a runtime library for [@rogu/tracker-codegen](). However, you still need to setup the provider manually to make it works.

## Overview

![tracker](./img/ss.png)

## Tracker Provider

Tracker Provider is a collection of modules that act as a container for your tracking service.

```ocaml
module __Your_Tracking_Service__ = {
  type t;
  let handler: t => unit
}
```

### Full Example

```ocaml
module Firebase = {
  type t = [ | `firebase];
  let handler = properties => {
    fun
    | `firebase => {
        Firebase.Analytics.make().logEvent(.
          properties##eventType,
          properties##context,
        );
      };
  };
};

module Internal = {
  type t = [ | `internal];

  let transform = properties =>
    Js.Json.[
      ("clientTimestamp", string @@ Js.Date.toString @@ Js.Date.make()),
      ("appVersion", string("0.0")),
      ("clientDevice", string("WEB")),
      ("clientOS", string("")),
      ("clientOSVersion", string("")),
      ("context", properties##context),
      ("deviceId", string("")),
      ("eventType", string @@  properties##eventType),
      ("eventVersion", number(0.0)),
      ("cookiesId", string @@ Session.make()),
      ("isLogged", boolean @@ false),
      ("memberId", string @@ UOC.get()),
      ("sessionId", string @@ Session.make()),
    ]
    |> Js.Dict.fromList
    |> Js.Json.object_
    |> Js.Json.stringify;

  let handler = properties =>
    fun
    | `internal => {
        Bs_fetch.(
          fetchWithInit(
            Config.config.apiEndpoint ++ "/event/tracker",
            RequestInit.make(
              ~method_=Post,
              ~body=BodyInit.make @@ transform(properties),
              (),
            ),
          )
        )
        |> ignore;
      };
};
```

## Tracker Module

This is the main module where you need to compose between the code from code generator and your own provider implementation.

```ocaml
module Make = RgTracker.Runtime.Make(LMS__Tracker);

include Make({

  type t = [ TrackerProvider.Internal.t | TrackerProvider.Firebase.t];

  let required = [|`internal|];

  let handler = properties =>
    fun
    | `internal as provider => TrackerProvider.Internal.handler(properties, provider)
    | `firebase as provider => TrackerProvider.Firebase.handler(properties, provider);
});

```

After you setup those two modules. You can use tracker as followings:

```ocaml

[@react.component]
let make = () => {
  <Button onPress={_ => Tracker.make(`generatedEvent(some_payload))}>
  </Button>
}
```

That code will only fire `internal` since it's the only required tracker to track.

However, if you want to track internal and firebase. You can do followings:

```ocaml
[@react.component]
let make = () => {
  <Button onPress={_ => Tracker.make(~providers=[|`firebase|],`generatedEvent(some_payload))}>
  </Button>
}
```

---
_Source: https://npm.io/package/@rogu/tracker-runtime · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
