# june-sdk

> This is June's JS SDK, based on Analytics-Next by Segment.

Latest version **2.0.0** (published 2022-02-09) · MIT license · 0 weekly downloads

## Install

```sh
npm install june-sdk
pnpm add june-sdk
yarn add june-sdk
bun add june-sdk
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2022-02-09 |
| First published | 2022-01-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 11 |
| Unpacked size | 3.2 MB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Maintainers | matteing |

## Links

- npm: https://www.npmjs.com/package/june-sdk
- npm.io page: https://npm.io/package/june-sdk

## Dependencies (11)

- [dset](https://npm.io/package/dset.md) ^3.0.0
- [tslib](https://npm.io/package/tslib.md) ^2.1.0
- [unfetch](https://npm.io/package/unfetch.md) ^4.1.0
- [encoding](https://npm.io/package/encoding.md) ^0.1.13
- [js-cookie](https://npm.io/package/js-cookie.md) ^2.2.1
- [spark-md5](https://npm.io/package/spark-md5.md) ^3.0.1
- [node-fetch](https://npm.io/package/node-fetch.md) ^2.6.1
- [@lukeed/uuid](https://npm.io/package/@lukeed/uuid.md) ^2.0.0
- [@segment/tsub](https://npm.io/package/@segment/tsub.md) ^0.1.9
- [@segment/facade](https://npm.io/package/@segment/facade.md) 3.4.7
- [@segment/analytics.js-video-plugins](https://npm.io/package/@segment/analytics.js-video-plugins.md) ^0.2.1

## Recent versions

- 2.0.0 (latest) — 2022-02-09
- 1.0.0-alpha1 — 2022-01-18
- 1.0.0-a — 2022-01-17
- 1.0.0 — 2022-01-17

## README

# June's SDK

This is June's JS SDK, based on Analytics-Next by Segment.

In a nutshell, there's tons of small fixes for compatibility.

## Notes

- `make dev` should start a development server after a `yarn install`.
- To use this locally, go into `src/browser.ts` and comment out line 62. It should start redirecting all requests to your local API.
- The writeKey is a valid June API key. You can make one in interim by going into the database (`api_keys` table) and inserting a new row.

# Original documentation

### Table of Contents

- [🏎️ Quickstart](#-quickstart)
  - [💡 Using with Segment](#-using-with-segment)
  - [💻 Using as an NPM package](#-using-as-an-npm-package)
- [🔌 Plugins](#-plugins)
- [🐒 Development](#-development)
- [🧪 Testing](#-testing)
  - [✅ Unit Testing](#-unit-testing)

---

# 🏎️ Quickstart

The easiest and quickest way to get started with Analytics 2.0 is to [use it through Segment](#-using-with-segment). Alternatively, you can [install it through NPM](#-using-as-an-npm-package) and do the instrumentation yourself.

## 💡 Using with Segment

1. Create a javascript source at [Segment](https://app.segment.com) - new sources will automatically be using Analytics 2.0! Segment will automatically generate a snippet that you can add to your website. For more information visit our [documentation](https://segment.com/docs/connections/sources/catalog/libraries/website/javascript/)).

2. Start tracking!

## 💻 Using as an NPM package

1. Install the package

```sh
# npm 
npm install @segment/analytics-next

# yarn
yarn add @segment/analytics-next

#pnpm
pnpm add @segment/analytics-next
```

2. Import the package into your project and you're good to go (with working types)! Example react app:

```ts
import { Analytics, AnalyticsBrowser, Context } from '@segment/analytics-next'
import { useEffect, useState } from 'react'
import logo from './logo.svg'

function App() {
  const [analytics, setAnalytics] = useState<Analytics | undefined>(undefined)
  const [writeKey, setWriteKey] = useState('<YOUR_WRITE_KEY>')

  useEffect(() => {
    const loadAnalytics = async () => {
      let [response] = await AnalyticsBrowser.load({ writeKey })
      setAnalytics(response)
    }
    loadAnalytics()
  }, [writeKey])

  return (
    <div className="App">
      <header className="App-header">
        <img src={logo} className="App-logo" alt="logo" />
        <p>
          Edit <code>src/App.js</code> and save to reload.
        </p>
        <a
          onClick={(e) => {
            e.preventDefault()
            analytics?.track('Hello world')
          }}
          className="App-link"
          href="https://reactjs.org"
          target="_blank"
          rel="noopener noreferrer"
        >
          Track
        </a>
      </header>
    </div>
  )
}

export default App
```

### using `Vite` with `Vue 3`

1. add to your `index.html` 

```html
<script>
  window.global = window
  var exports = {}
</script>
```

2. create composable file `segment.ts` with factory ref analytics:

```ts
import { ref, reactive } from 'vue'
import { Analytics, AnalyticsBrowser } from '@segment/analytics-next'

const analytics = ref<Analytics>()

export const useSegment = () => {
  if (!analytics.value) {
    AnalyticsBrowser.load({
      writeKey: '<YOUR_WRITE_KEY>',
    })
      .then(([response]) => {
        analytics.value = response
      })
      .catch((e) => {
        console.log('error loading segment')
      })
  }

  return reactive({
    analytics,
  })
}

```

3. in component 

```vue
<template>
  <button @click="track()">Track</button>
</template>

<script>
import { defineComponent } from 'vue'
import { useSegment } from './services/segment'

export default defineComponent({
  setup() {
    const { analytics } = useSegment()
    
    function track() { 
      analytics?.track('Hello world')
    }
    
    return {
      track
    }
  }
})
</script>

```


# 🐒 Development

First, clone the repo and then startup our local dev environment:

```sh
$ git clone git@github.com:segmentio/analytics-next.git
$ cd analytics-next
$ make dev
```

Then, make your changes and test them out in the test app!

<img src="https://user-images.githubusercontent.com/2866515/135407053-7561d522-b969-484d-8d3a-6f1c4d9c025b.gif" alt="Example of the development app" width="500px">

# 🔌 Plugins

When developing against Analytics Next you will likely be writing plugins, which can augment functionality and enrich data. Plugins are isolated chunks which you can build, test, version, and deploy independently of the rest of the codebase. Plugins are bounded by Analytics Next which handles things such as observability, retries, and error management.

Plugins can be of two different priorities:

Critical: Analytics Next should expect this plugin to be loaded before starting event delivery
Non-critical: Analytics Next can start event delivery before this plugin has finished loading

and can be of five different types:

Before: Plugins that need to be run before any other plugins are run. An example of this would be validating events before passing them along to other plugins.
After: Plugins that need to run after all other plugins have run. An example of this is the segment.io integration, which will wait for destinations to succeed or fail so that it can send its observability metrics.
Destination: Destinations to send the event to (ie. legacy destinations). Does not modify the event and failure does not halt execution.
Enrichment: Modifies an event, failure here could halt the event pipeline.
Utility: Plugins that change Analytics Next functionality and don't fall into the other categories.

Here is an example of a simple plugin that would convert all track events event names to lowercase before the event gets sent through the rest of the pipeline:

```ts
export const lowercase: Plugin = {
  name: 'Lowercase events',
  type: 'before',
  version: '1.0.0',

  isLoaded: () => true,
  load: () => Promise.resolve(),

  track: (ctx) => {
    ctx.event.event = ctx.event.event.toLowerCase()
    return ctx
  },
  identify: (ctx) => ctx,
  page: (ctx) => ctx,
  alias: (ctx) => ctx,
  group: (ctx) => ctx,
  screen: (ctx) => ctx,
}
```

For further examples check out our [existing plugins](https://github.com/segmentio/analytics-next/tree/master/src/plugins).

# 🧪 Testing

The tests are written in [Jest](https://jestjs.io) and can be run be using `make test-unit`
Linting is done using [ESLint](https://github.com/typescript-eslint/typescript-eslint/) and can be run using `make lint`.

## ✅ Unit Testing

Please write small, and concise unit tests for every feature you work on.

```sh
$ make test-unit # runs all tests
$ yarn jest src/<path> # runs a specific test or tests in a folder
```

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