# Getting Started

Learn how to start using Plaudy in minutes.

## Sign up

Head over to [app.plaudy.com/signup](https://app.plaudy.com/signup) to create an account and [get your api key](https://app.plaudy.com/settings/workspace/keys). That's it! 🔥

## Install

Install plaudy using `yarn` or `npm`:

```javascript
// Using npm
npm install plaudy

// or yarn
yarn add plaudy
```

## Import

One installed, use a simple `require` or `import` statement:

```javascript
import Plaudy from 'plaudy';
```

## Initialize

Init your workspace with `.init()`, where `` `YOUR API KEY` `` is replaced with [the API Key in your dashboard.](https://app.plaudy.com/settings/keys)

```javascript
Plaudy.init(`YOUR_API_KEY`);
```

{% hint style="success" %}
Make sure to replace `YOUR_API_KEY` with your actual key or Plaudy will not track any events for you.&#x20;
{% endhint %}

## Identify (optional)

Identify the user and/or object that will be tracked for subsequent events. This benefits you by allowing you to skip passing these fields in for every single event tracked. However, you can always pass these fields through when calling `.track()` .\
\
`USER_ID` is your customer that you want to track this event for. `OBJECT_ID` is a secondary identifier for when the user has multiple spaces/workspaces/locations within a single account:

```javascript
Plaudy.identify(`USER_ID`, `OBJECT_ID`);
```

{% hint style="info" %}
It is possible to track events without identifying the user. In this case, the tracked user recorded for the event will be `no_tracked_user`. This allows you to still have query-friendly events for things like overall app usage, button clicks on your landing page, etc. We've done this on [our landing page](https://plaudy.com#demo) as an example.&#x20;
{% endhint %}

Now you know how to get it set up. It's time to [start tracking events](/tracking-analytics)! :raised\_hands:&#x20;


# Tracking Analytics

Learn how to begin tracking analytic events

## Track

Once you're set up, tracking events is easy as 🥧:

```javascript
Plaudy.track("purchase", {
    "amount": 413.25,
    "upsell": 63.25,
    "referrer": "landing page",
    "items": [
        "dehydrated h20",
        "non-gmo water",
        "filtered dirt"
    ],
    "user_id": `USER_ID` // if you didn't pass this in when using .identify()
    "object_id": `OBJECT_ID` // if you didn't pass this in when using .identify()
    // anything else
});
```

The first argument is the `event_type`, which you can call whatever you'd like. The second argument is any data you want to pass in (up to 1,000 key-value pairs).

{% hint style="info" %}
`count` is a preserved keyword and will determine how much the counter for this event type and user increments because of this event. In most cases, you will not need to pass this in.
{% endhint %}

## Examples

### 1. Tracking A Page View

Here's a simple example of everything together in a file we'll call `index.js`. We'll be tracking a single page view:

{% code title="index.js" %}

```javascript
import Plaudy from 'plaudy';

Plaudy.init(`YOUR_API_KEY`);
Plaudy.identify(`USER_ID`, `OBJECT_ID`);

Plaudy.track("page view", {
    "title": window.location.hostname,
    "url": "https://docs.plaudy.com"
});
```

{% endcode %}

That's it! You successfully tracked your first page view for this user! But there's so much more you can do! [Keep it going](/displaying-analytics)! 🎉


# Displaying Analytics

Learn how to begin displaying analytic events

## Count

Count is powerful but surprisingly simple. This is the most performant call you can make when querying analytics. Any required fields or custom data fields are queryable, with the main fields to keep in mind being `object_id` [when applicable](/#identify-optional) and `events`.

When performing any query type, we always use `events` as an array of event names or objects (if more complex querying) like so:

```javascript
await Plaudy.count({
    "events": [
        "purchase",
        "click",
        "visit"
    ],
    "timeframe": {
        "start": 1597079145327,
        "end": +new Date()
    },
    "user_id": "USER_ID" // if you didn't pass this in when using .identify()
    "object_id": "OBJECT_ID" // if you didn't pass this in when using .identify()
});
```

### Options

| Name       | Type           | Default             | Values                                                                                                                                                                                                                                                                          | Required |
| ---------- | -------------- | ------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- |
| events     | array          | \[ ]                | any                                                                                                                                                                                                                                                                             | true     |
| timeframe  | string, object | "all\_time"         | <p>\["today", "this\_day", "this\_week", "this\_month", "this\_year", "yesterday", "last\_day", "last\_week", "last\_month", "last\_year"]<br></p><p>OR</p><p></p><p>An object with both <code>start</code> and <code>end</code> </p><p>as unix timestamps (see code above)</p> | false    |
| user\_id   | string         | "no\_tracked\_user" | any                                                                                                                                                                                                                                                                             | false    |
| object\_id | string         | "no\_object\_id"    | any                                                                                                                                                                                                                                                                             | false    |

{% hint style="success" %}
This method takes advantage of daily and total caching. It is built to be highly performant and snappy up to trillions of events queried.
{% endhint %}

## Examples

### 1. Count page views today

Here's an example in a single file we'll call `query-today.js`. We'll be getting a count of today's page views:

{% code title="query-today.js" %}

```javascript
import Plaudy from 'plaudy';

Plaudy.init(`YOUR_API_KEY`);
Plaudy.identify(`USER_ID`, `OBJECT_ID`);

const counts = await Plaudy.count({
    "events": [
        "page view"
    ],
    "timeframe": "today",
});

console.log('COUNTS:', counts);
// COUNTS: { `OBJECT_ID`: {"page view": 1492} }
```

{% endcode %}

### 2. Count clicks and page views this week

Here's an example in a single file we'll call `query-week.js`. We'll be getting a count of today's page views:

{% code title="query-week.js" %}

```javascript
import Plaudy from 'plaudy';

Plaudy.init(`YOUR_API_KEY`);
Plaudy.identify(`USER_ID`, `OBJECT_ID`);

const counts = await Plaudy.count({
    "events": [
        "click",
        "page view"
    ],
    "timeframe": "this_week",
});

console.log('COUNTS:', counts);
// COUNTS: { `OBJECT_ID`: {"click": 42, "page view": 525600} }
```

{% endcode %}

Aaaand just like that, you have working customer-facing analytics! 💥Applying this gives you unlimited potential for customer engagement through metrics.

If you have any questions, feel free to reach out any time if you need help at <hi@plaudy.com>.


