<h1 align="center">DSC JS Utils</h1>
<h5 align="center">JS utilities to be used across JS repositories.</h5>

---

[![CircleCI](https://circleci.com/gh/dollarshaveclub/js-utils/tree/master.svg?style=svg&circle-token=95a4c45c671495bb3964c90777ad743aa59d51b0)](https://circleci.com/gh/dollarshaveclub/js-utils/tree/master)
[![Greenkeeper badge](https://badges.greenkeeper.io/dollarshaveclub/js-utils.svg?token=510b03d16ca109649e6bd747e714fa2155259de718d7b40a443c771335fbd3a6&ts=1510134184855)](https://greenkeeper.io/)

---

<p align="center">
  <a href="#folders">Folders</a>&nbsp;&nbsp;
  <a href="#usage">Usage</a>&nbsp;&nbsp;
  <a href="#analytics">Analytics</a>&nbsp;&nbsp;
  <a href="#publishing">Publishing</a>&nbsp;&nbsp;
  <a href="https://github.com/dollarshaveclub/js-utils/tree/master/browser/README.md">Browser Docs</a>&nbsp;&nbsp;
  <a href="https://github.com/dollarshaveclub/js-utils/tree/master/isomorphic/README.md">Isomorphic Docs</a>
</p>

---

⚠️⚠️ **NOTE**: this package is published publicly, so do not put any sensitive information in this repository. ⚠️⚠️

## Folders

There are 2 key folders which contain utility scripts—browser and isomorphic. Browser utility scripts are meant to be used within a browser. Isomorphic utility scripts are scripts that can be run within any environment.

### Browser

Browser-specific utilities scripts.

- **Uses ES6 Modules** import/export syntax. Read more about [ES6 Modules](http://2ality.com/2014/09/es6-modules-final.html). [Read more](https://github.com/dollarshaveclub/js-utils/tree/master/browser/README.md).

### Isomorphic

Utility scripts that work in any JavaScript environment. [Read more](https://github.com/dollarshaveclub/js-utils/tree/master/isomorphic/README.md).

- **Use CommonJS** node module syntax. Read more about [CommonJS](https://addyosmani.com/writing-modular-js/).

---

<h2 id="usage">Using JS Utils</h2>

Installing NPM:

```bash
npm i --save @dollarshaveclub/js-utils
```

Import the built versions of files from the build folder:

```js
const {
  SHOP_CODES,
  ENVIRONMENTS,
} = require('@dollarshaveclub/js-utils/build/isomorphic/config')
```

You may be able to use the non-built versions in server environments,
but always use the built versions in browser environments.

---

<h2 id="analytics">Analytics Integration</h2>

If interfacing with [Tracksuit](https://github.com/dollarshaveclub/tracksuit), initialize analytics in the following way.

**Initialize once:**

```javascript
// src/bootstrap.js

import bootstrap from '@dollarshaveclub/js-utils/build/browser/bootstrap-data'
import { initializeAnalytics } from '@dollarshaveclub/js-utils/build/browser/analytics'

const faceAssetsFixtures = window.FaceAssetsFixtures
const { study, features } = bootstrap(faceAssetsFixtures)

// Be sure to provide Fixtures, AB Tests, and your specified vendors. Also be
// sure to export this constant if you wish to interface with your analytics
export const analytics = initializeAnalytics(
  faceAssetsFixtures,
  study.assignments(),
  {
    DTM: true,
    GTM: true,
  }
)
```

**Access the export in other files**

```javascript
// app/home/index.js

import { analytics } from '../../bootstrap'

analytics.call('page') // Track page information
analytics.call('trackEvent', {
  event: 'butt',
  category: 'Link Click',
  action: 'movies',
  label: 'The Room',
  value: '',
})
```

The `analytics` API uses Tracksuit under the hood. You may pass any Tracksuit
function name to the `analytics.call` function to execute it. All parameters are
retained.

---

## Publishing

Automatically updated and published when a release is cut. Also after a successful deploy, please be sure to update the version number and update [face-web](https://github.com/dollarshaveclub/face-web) as well.

---
