Was this page helpful?

Segment plugin

Segment is a powerful customer data orchestration tool. This plugin allows you to send experience views to a Segment as track events via a connected Analytics.js source.

Installation

Install the dependency:

npm install @ninetailed/experience.js-plugin-segment
yarn add @ninetailed/experience.js-plugin-segment

Then, add the plugin to the Ninetailed instance:

import { NinetailedSegmentPlugin } from '@ninetailed/experience.js-plugin-segment'
<NinetailedProvider
  // ...
  plugins={[
    new NinetailedSegmentPlugin()
  ]}
  componentViewTrackingThreshold={2000} // (Optional prop) Number, default = 2000
>
    //...
</NinetailedProvider>
plugins: [ 
    // ...
    {
        resolve: `@ninetailed/experience.js-gatsby`,
        options: {
            // ...
            componentViewTrackingThreshold: 2000, // (Optional prop) Number, default = 2000
            ninetailedPlugins: [
                {
                    resolve: `@ninetailed/experience.js-plugin-segment`,
                    options: {}
                }    
            ]
        }
    }
]
import { Ninetailed } from '@ninetailed/experience.js';
import { NinetailedSegmentPlugin } from '@ninetailed/experience.js-plugin-segment'

export const ninetailed = new Ninetailed(
    { 
        clientId: // Your client ID 
        environment: // Your Ninetailed environment
    }, 
    { 
        plugins: [
            new NinetailedSegmentPlugin()
        ],

        // Specify an amount of time (ms) that a component must be present in the viewport to register a component view
        componentViewTrackingThreshold: 2000,
    }
);

Timing configuration

The Insights Plugin logs that a component has been seen only after the component has remained within the user's viewport for a specified amount of time (in milliseconds), determined by the value of the componentViewTrackingThreshold property on the Ninetailed instance (see code samples above). If the option is unspecified, the value defaults to 2000.

Default data layer properties

Events sent from this plugin are named nt_experience. They are sent with five default parameters:

ParametersValue TypeValue Description
ninetailed_experienceStringExperience ID of the experience
ninetailed_experience_nameStringTitle of the experience entry from the CMS
ninetailed_variantString"control", "variant 1", "variant 2", …
ninetailed_audienceStringCMS entry ID of the audience
ninetailed_componentStringCMS entry ID of the shown variant

Custom event properties

You can also define your own variables to push to Segment by passing in a configuration object when instantiating the plugin. To do so, define a config object with a template property whose value is an object consisting of key-value pairs, where the key is the name of the property you want to add to the Segment track event and the value is a string of the desired variable value surrounded by double curly braces ({{ }}).

Available properties

PropertyNotes
experience.id
experience.typent_experiment or nt_personalization
experience.name
experience.description
audience.idALL_VISITORS if not set
audience.nameAll Visitors if not set
audience.description
selectedVariant
selectedVariant.YOUR_PROPSpecify any property key from the selected experience variant
selectedVariantIndex0, 1, 2, etc.
selectedVariantSelector"control", "variant 1", "variant 2", etc. This is a mapping of selectedVariantIndex from above.

Example custom use

This example shows passing the human-readable name of an audience to a custom event property titled ninetailed_audience_name. The default event properties are also pushed.

<NinetailedProvider
   // ...
    plugins={[
      new NinetailedSegmentPlugin({
        template: {
          ninetailed_audience_name: '{{ audience.name }}',
        },
      })
     ]}
     componentViewTrackingThreshold={2000}
  >
   //...
 </NinetailedProvider>
plugins: [
    // ...
    {
        resolve: `@ninetailed/experience.js-gatsby`,
        options: {
            // ...
            componentViewTrackingThreshold: 2000,
            ninetailedPlugins: [
                {
                    resolve: `@ninetailed/experience.js-plugin-segment`,
                    options: {
                        template: {
                            ninetailed_audience_name: '{{ audience.name }}',
                        }
                    }
                }    
            ]
        }
    }
]
import { Ninetailed } from '@ninetailed/experience.js';
import { NinetailedSegmentPlugin } from '@ninetailed/experience.js-plugin-segment'

export const ninetailed = new Ninetailed(
    { 
        clientId: // Your client ID 
        environment: // Your Ninetailed environment
    }, 
    { 
        plugins: [
            new NinetailedSegmentPlugin({
              template: {
                ninetailed_audience_name: '{{ audience.name }}',
              },
            })
        ],

        // Specify an amount of time (ms) that a component must be present in the viewport to register a component view
        componentViewTrackingThreshold: 2000,
    }
);