# @alexanderjeurissen/use-reactga

> ReactGA provider / hook for consuming reactga anywhere in the render tree.

Latest version **2.0.2** (published 2020-08-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install @alexanderjeurissen/use-reactga
pnpm add @alexanderjeurissen/use-reactga
yarn add @alexanderjeurissen/use-reactga
bun add @alexanderjeurissen/use-reactga
```

## Health

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

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.2 |
| Published | 2020-08-06 |
| First published | 2020-06-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=10.0.0 |
| Dependencies | 2 |
| Unpacked size | 6.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Alexander Jeurissen |
| Maintainers | alexanderjeurissen |
| Keywords | reactga, google-analytics, provider, hooks, hook, react, reactjs, jsx, component |

## Links

- npm: https://www.npmjs.com/package/@alexanderjeurissen/use-reactga
- Repository: git@github.com:alexanderjeurissen/use-reactga
- Issues: https://github.com/alexanderjeurissen/use-reactga/issues
- npm.io page: https://npm.io/package/@alexanderjeurissen/use-reactga

## Dependencies (2)

- [react](https://npm.io/package/react.md) ^16.13.1
- [react-ga](https://npm.io/package/react-ga.md) ^3.0.0

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 2.0.2 (latest) — 2020-08-06
- 2.0.1 — 2020-06-24
- 2.0.0 — 2020-06-24
- 1.0.4 — 2020-06-23
- 1.0.3 — 2020-06-23
- 1.0.2 — 2020-06-21
- 1.0.1 — 2020-06-21

## README

# useReactGA - provider & hook
![Publish](https://github.com/alexanderjeurissen/use-reactga/workflows/Publish/badge.svg)

This library provides a provider and hook to consume the initialized `ReactGA` context anywhere in the render tree.
It's removes the need to initialize ReactGA multiple times, and provides a way for react components to hook into an existing ReactGA instance.

### Installation

either with NPM or Yarn:

`yarn add @alexanderjeurissen/use-reactga`

or

`npm install --save @alexanderjeurissen/use-reactga`

### Basic usage

In most standalone React applications you likely want to decorate your whole application, or a submodule of your application. For this usecase a provider is provided:

```jsx
import { ReactGAProvider } from '@alexanderjeurissen/use-reactGA';

const App = () => (
  <ReactGAProvider>
    <BrowserRouter>
      ...
    </BrowserRouter>
  </ReactGAProvider>
);

export default App;
```

and use anywhere in the render tree:

```jsx
import { useReactGA } from '@alexanderjeurissen/use-reactGA';

const Component = () = {
  const { event } = useReactGA();
  
  return (
    <input onCopy={e => {
      event({ category: 'SomeCategory', action: "Copy", value: e.target.value.length });
    }} />
  )
}
```

### ReactGA HOC

There are situations where you want to decorate the result of a HOC, or where hooking the provider into existing callbacks and decorators is desired.
For those usecases a `withReactGAContext` HOC is provided. Here is an example of hooking the provider with Storybook, by passing it in the `addDecorator` method.

```js
import { withReactGAContext } from '@alexanderjeurissen/use-reactga';

addDecorator(storyFn => withReactGAContext(storyFn, 'UA-000000-01'));
```

The `withReactGAContext` hoc consumes the same Provider as described in the previous example. It's a wrapper to allow the provider to be consumed in a functional fashion.

### ReactGAProvider with custom ReactGA

You might run into situations where more fine-grained control is desired over the value that is passed to the provider.
For instance if you use a custom/version pinned ReactGA, or if you'd like to use your local ReactGA version instead of the version packed with this library.
Therefore one can pass their *own* ReactGa module to the provider as a prop. The resulting context will use the ReactGA module that is passed into it, and fallback to the ReactGA version that is shipped with this library.

```js
import { ReactGAProvider } from '@alexanderjeurissen/use-reactGA';
import ReactGA from 'react-ga';

const App = () => (
  <ReactGAProvider ReactGA={ReactGA}>
    <BrowserRouter>
      ...
    </BrowserRouter>
  </ReactGAProvider>
);

export default App;
```

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