npm package: @ninetailed/gatsby-plugin-ninetailed
Description: Ninetailed Gatsby Plugin for dynamic content, personalization and a/b testing.
Latest version: 1.1.0-alpha.2

# @ninetailed/gatsby-plugin-ninetailed

Add dynamic content personalization to Gatbsy without performance trade-offs or complex integrations.

`gatsby-plugin-ninetailed` is a React component specially designed to work seamlessly with
Gatsby and Ninetailed.

**[Demo](https://demo.ninetailed.io/saas)**

## Table of Contents

- [Ninetailed Platform](#ninetailed-platform)
- [Install](#install)
  - [Gatsby](#gatsby)
  - [Gatsby and Contentful](#gatsby-and-contentful)
- [How to use](#how-to-use)
- [Personalizing Components](#personalizing-components)
  - [Personalize Component](#personalize-component)
  - [Inline Personalization](#inline-personalization)
- [Contentful Richtext](#contentful-richtext)
- [Additional Documentation](#additional-documentation)

## Ninetailed Platform

Ninetailed is an api-first optimization platform designed for the modern web. It is a headless personalization solution for frameworks like React or Gatsby JS and headless CMS like Contentful. The **[Ninetailed](https://ninetailed.io/)** platform includes:

- Real-time personalization API and customer data hub (CDP).
- Plugins and SDKs for easy integration with major frameworks like Gatsby.
- CMS integrations to enable content creators easily create personalized content and define audiences.
- Integration with analytics tools like Google Analytics and Mixpanel.
- Optional edge side rendering for maximum web performance.

## Install

### Gatsby

`npm install @ninetailed/gatsby-plugin-ninetailed`

Install the `gatsby-plugin-ninetailed` modules via npm or yarn.

**Install module via npm**

```shell
npm install @ninetailed/gatsby-plugin-ninetailed
```

**Install module via yarn**

```shell
yarn add @ninetailed/gatsby-plugin-ninetailed
```
### Gatsby and Contentful

If you use Gatsby and Contentful as CMS install the `gatsby-plugin-ninetailed` and `contentful-plugin-ninetailed` modules via npm or yarn.

**Install module via npm**

```shell
npm install @ninetailed/gatsby-plugin-ninetailed @ninetailed/client-sdk-gatsby-contentful
```

**Install module via yarn**

```shell
yarn add @ninetailed/gatsby-plugin-ninetailed @ninetailed/client-sdk-gatsby-contentful
```

## How to use

Add the plugin to the plugins array in your gatsby-config.js and your API Key.

```js
plugins: [
    ...your other gatsby plugins
    {
        resolve: `gatsby-plugin-ninetailed`,
        options: {
            apiKey: 'your api key'
        }
    }
]
```

> Your API Key can be found in the Contentful app configuration.

By using the gatsby plugin there's no need to configure the `ProfileProvider` or `AnalyticsProvider` as described in the React tutorial, as this is done by the plugin.

The plugin automatically tracks Pageviews on route change, please do not track it on your own as you would generate incorrect events.

## Personalizing Components

### Personalize Component

To make personalizing your components as seamless as possible Ninetailed provides a `<Personalize />` component which wraps the component you'd like to personalize. It automatically detects the properties needed from the wrapped component and also applies a `variants` property.

```TypeScript
import React from 'react';
import { Personalize } from '@ninetailed/client-sdk-react';

type HeadlineProps = {
  text: string;
}

const Headline: React.FC<HeadlineProps> = ({ text }) => {
  return <h1>{text}</h1>
};

export const Page = () => {
  // These variants normally come from your CMS
  // You can use our Contentful App for this
  const variants = [
    {
      text: "We build super nice websites for enterprise companies!", 
      audience: "enterprise-audience-id" // variants have a audience id
    }
  ]
  
  return (<Personalize 
    component={Headline} 
    variants={variants} 
    text="We build websites for everbody" // this is the baseline for user which are not in a audience. 
  />);
};
```

### Inline Personalization

Using the visitor's and the useProfile hook makes it very easy to use inline personalization. For advanced cases like contentful Richtext we also provide a SDK - simply have a look at the gatsby section.

```TypeScript
import React, { useState, useEffect } from 'react';
import { useProfile } from '@ninetailed/client-sdk-react';

const Greeting = () => {
  const [loading, profile, error] = useProfile();

  if (loading) {
    return <h2>Hey              👋, how is your day?</h2>
  }

  return <h2>Hey {profile.traits.firstname}👋, how is your day?</h2>
};
```

## Contentful Richtext

The Ninetailed Contentful App gives your content creators the option to use inline personalization in richtext fields. This is done by adding embeded entries into the text. To make the personalization working on the developer end the `@ninetailed/client-sdk-gatsby-contentful` SDK provides functions which render the personalized entry. 

```TypeScript
import React from 'react';
import {
  renderRichText,
  RenderRichTextData,
  ContentfulRichTextGatsbyReference,
} from 'gatsby-source-contentful/rich-text';
import { createRenderNinetailedMergetag } from '@ninetailed/client-sdk-gatsby-contentful';

const textOptions = (profile: Profile) => ({
  renderNode: {
    // ... your own richtext render options
    ...createRenderNinetailedMergetag(profile),
  },
});

type HeadlineProps = {
  text: RenderRichTextData<ContentfulRichTextGatsbyReference>;
};

const Headline: React.FC<HeadlineProps> = ({ text }) => {
  const [loading, profile, error] = useProfile();

  return <h1>{renderRichText(text, textOptions(profile))}</h1>
}
```

## Additional Documentation

**[Documentation](https://docs.ninetailed.io/)**