npm package: @onthebarrelhead/lending-analytics
Description: A package for NerdWallet's lending single threaded team (STT) that provides analytics tracking capabilities. This package is designed to be installed as an NPM module and requires all dependencies to be installed as peer dependencies.
Author: Roi Kingon
Latest version: 0.17.7

# @onthebarrelhead/lending-analytics

A package for NerdWallet's lending single threaded team (STT) that provides analytics tracking capabilities. This package is designed to be installed as an NPM module and requires all dependencies to be installed as peer dependencies.

## Docs

[View the typedocs](https://friendly-disco-49zgv1n.pages.github.io)  
[View the example client](https://github.com/NerdWallet-Lending/lending-analytics-example)  
[View the changelog](https://github.com/NerdWallet-Lending/lending-analytics/blob/main/CHANGELOG.md)

## Installation

You can install this package using NPM with the following command:

```bash
yarn add @onthebarrelhead/lending-analytics
```

## Usage

### AnalyticsProvider

The `AnalyticsProvider` component is used to wrap your application with an analytics context that provides access to the analytics object. The component takes the following props:

- `deployableName` (required): The name of the deployable.
- `deployableVersion` (required): The version of the deployable.
- `writeKey` (optional): The write key for Segment analytics. If this prop is not provided, analytics will be logged to the console instead.
- `debug` (optional): A boolean value indicating whether debug information should be displayed.
- `children` (required): The child components to be wrapped with the analytics context.

Example:

```tsx
import { AnalyticsProvider } from '@onthebarrelhead/lending-analytics'

const App = () => {
  return (
    <AnalyticsProvider deployableName="MyApp" deployableVersion="1.0.0" writeKey="YOUR_SEGMENT_WRITE_KEY">
      <MyComponent />
    </AnalyticsProvider>
  )
}
```

### useAnalytics

The `useAnalytics` hook provides access to the analytics object within a component wrapped by the `AnalyticsProvider`. Example usage:

```tsx
import { useAnalytics } from '@onthebarrelhead/lending-analytics'

const MyComponent = () => {
  const { analytics } = useAnalytics()

  const handleClick = () => {
    analytics.track('Button Clicked')
  }

  return <button onClick={handleClick}>Click Me!</button>
}
```

### AnalyticsPageView

The `AnalyticsPageView` component is used to track page views within your application. The component takes the following props:

- `name` (optional): The name of the page view.
- `contentId` (optional): The ID of the content being viewed.
- `contentSource` (optional): The source of the content being viewed.
- `children` (required): The child components to be tracked as part of the page view.

Example:

```tsx
import { AnalyticsPageView } from '@onthebarrelhead/lending-analytics'

const MyPage = () => {
  return (
    <AnalyticsPageView name="My Page">
      <MyComponent />
    </AnalyticsPageView>
  )
}
```

### useAnalyticsPageView

The useAnalyticsPageView hook provides access to the current page view context within a component wrapped by the AnalyticsPageView. Example usage:

```tsx
import { useAnalyticsPageView } from '@onthebarrelhead/lending-analytics'

const MyComponent = () => {
  const { pageViewId, pageName } = useAnalyticsPageView()

  return (
    <div>
      <h1>{pageName}</h1>
      <p>Page view ID: {pageViewId}</p>
    </div>
  )
}
```

### AnalyticsSkuViewed

The AnalyticsSkuViewed component is used to track SKU impressions within your application. The component takes the following props:

- `id`: ID of the viewed SKU
- `source`: Source of the viewed SKU
- `name`: Name of the viewed SKU
- `productType`: Type of product associated with the viewed SKU
- `level`: Level of the viewed SKU
- `institutionId` (optional): ID of the institution associated with the viewed SKU
- `institutionName` (optional): Name of the institution associated with the viewed SKU
- `productId` (optional): ID of the product associated with the viewed SKU
- `productName` (optional): Name of the product associated with the viewed SKU
- `offerId` (optional): ID of the offer associated with the viewed SKU
- `offerName` (optional): Name of the offer associated with the viewed SKU
- `monetizable`: Whether or not the viewed SKU is monetizable. This can also be overridden at the shopping finished level to provide more flexibility.
- `impressionSection`: Section of the website or app where the SKU was viewed
- `impressionPosition`: Position of the SKU within the impression section
- `children` (optional): Either a JSX element or a function that takes an AnalyticsSkuViewedContextValue context object as an argument and returns a JSX element.

Example:

```tsx
import { AnalyticsSkuViewed } from '@segment/analytics-react'

function ProductPage({ product }) {
  return (
    <AnalyticsSkuViewed
      id="097a5c08-4339-11ea-8c79-8bd12b91c634"
      name="Chase Freedom Unlimited Card"
      productType="credit_cards"
      level="offer"
      source="marketplace"
      productId="253b9c27-2948-12cb-8c79-8bd12b122c581"
      product_name="Freedom Unlimited Card"
      monetizable="yes_assumed"
      institutionId="124b6c08-1237-11ea-8c79-8bd12b12d429"
      institutionName="Chase"
      impressionSection="Best Credit Card Roundup"
      impressionPosition={2}
    >
      {({ fireShoppingFinishedEvent }) => {
        return (
          <ProductCard>
            <Button
              href={cta.url}
              onClick={() => {
                fireShoppingFinishedEvent({
                  monetizable: cta.monetizable,
                  destinationUrl: cta.url,
                  ctaType: 'APPLY_NOW'
                })
              }}
            >
              Apply Now
            </Button>
          </ProductCard>
        )
      }}
    </AnalyticsSkuViewed>
  )
}
```

## Versioning

Changesets is an npm module that helps with managing semantic versioning for packages. It provides a command-line interface (CLI) for creating and managing "changesets" – small files that describe changes made to a package since the last release. Changesets can be thought of as "mini-changelogs" that provide a clear and concise summary of the changes made to a package.

1. When contributing, use `yarn changeset` to describe your changes. The cli will walk you through the options and a summary.

```bash
yarn changeset
```

2. When ready to release, run: `yarn changeset:version` to automatically rev our package.json and generate a changelog