Using Aptabase Analytics with NativeScript
Tags: nativescript, aptabase, ios, android
Published: 2023-06-26 17:31:40

[Aptabase](https://aptabase.com/) is an open source analytics solution which "in the realms of user tracking and privacy, employs a unique strategy to secure the privacy of your users."

It can be used to track events and sessions to provide qualitative and quantitive analysis to your app's usage. Let's take a look at how to use it with NativeScript apps by demonstrating live on StackBlitz.

## Aptabase Demo on StackBlitz for iOS and Android

We can try Aptabase on iOS and Android right now via StackBlitz:
https://stackblitz.com/edit/nativescript-with-aptabase?file=app%2Fmain-view.ts

Tapping on the items in the list calls `Aptabase.track` to specify what event just took place.

{% youtube ly1curTpi_Q %}

When viewing Aptabase events from Stackblitz, be sure to switch the mode in the top right to 'Release' since it's being demonstrated from the live NativeScript Preview app:

![Aptabase Release Mode](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u5umzojbp0zz269b2os4.png)

We can then view the tracked events:

![Aptabase tracked events](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1zovlb1uoueb1a50nnug.png)

In order to see your own dashboard of events, you'll want to swap the `app.ts` call to `Aptabase.initialize` with your own api key which you can register to receive one [here](https://aptabase.com).

When setting up Aptabase in your own app, you can follow the instructions as outlined below.

## Install, Configure and use Aptabase

Install the SDK using your preferred JavaScript package manager:

```bash
pnpm add @nstudio/nativescript-aptabase
# or
npm add @nstudio/nativescript-aptabase
# or
yarn add @nstudio/nativescript-aptabase
```

For iOS, configure your `nativescript.config.ts` to use the Swift Package:

```ts
ios: {
    SPMPackages: [
        {
            name: 'Aptabase',
            libs: ['Aptabase'],
            repositoryURL: 'https://github.com/aptabase/aptabase-swift.git',
            version: '0.2.0'
        }
    ]
}
```

## Usage

Initialize the SDK using your `App Key` and the `initialize` function. We would recommend calling it as early as possible in your app life cycle. 


```ts
import { Aptabase } from '@nstudio/nativescript-aptabase';

Application.on(Application.launchEvent, () => {

  // this is where you enter your App Key👇
  Aptabase.initialize('<YOUR_APP_KEY>');

});
```

Afterwards you can start tracking events with `track`:

```js
import { Aptabase } from '@nstudio/nativescript-aptabase';

// Track an event
Aptabase.track("connect_click");
 
// Track events with custom properties
Aptabase.track("play_music", {
  name: "Here comes the sun"
});
```

A few important notes:

1. The SDK will automatically enhance the event with some useful information, like the OS, the app version, and other things.
2. You're in control of what is sent to Aptabase. This SDK does not automatically track any events, you need to call `track` manually.
   - Because of this, it's generally recommended to at least track an event at startup
3. You do not need to await the `track` function, it'll run in the background.
4. Only strings and numbers values are allowed on custom properties

Enjoy learning from the insightful data gathered from your NativeScript app's usage of Aptabase!