More utilities to integrate Ninetailed within your current tech stack.
Our Experience API utility libraries provide methods to map experience content to the format required by the <Experience> component exported by our React, Next.js, and Gatsby SDKs.
Use the Contentful Utility SDK if you are are retrieving content and experiences using Contentful's client libraries that interface with the Contentful REST APIs, including:
the Contentful Content Delivery API
the Contentful Content Preview API
For all other sources, including:
the Contentful GraphQL API
the Contentstack Content Delivery API
your own internal content APIs, middleware, etc.
use the JavaScript Utility SDK and map your experiences to the type required by the ExperienceMapper class methods.
You must map your fetched CMS Experience entries to a particular shape prior to transforming them with the ExperienceMapper methods. The following examples show the format required in a .map step prior to calling .filter to remove ill-formatted entries.
import { ExperienceMapper } from'@ninetailed/experience.js-utils';constmappedExperiences= (myEntry.nt_experiences || []).map((experience) => {return { id:experience.id, name:experience.name type: experience.nt_type as'nt_personalization'|'nt_experiment' config: experience.nt_config, audience: { id:experience.nt_audience.nt_audience_id// If mapping for the Preview Plugin, this displays audience names name: experience.nt_audience.nt_name }, variants:experience.variants.map((variant) => {return { id:variant.id,// Required// Map any other fields required by your components...variant, someComponentProp:variant.foo } }) } }).filter((experience) =>ExperienceMapper.isExperienceEntry(experience)).map((experience) =>ExperienceMapper.mapExperience(experience));
Your exact query and mapping will vary depending on both your content model and the props required by the component you use to render your content. This example assumes a content model using a content type of page that contains a field called sections that can reference entries of type hero. It also shows using a lightweight GraphQL client library graphql-request to make the API request, but any GraphQL client is suitable.
import { ExperienceMapper } from'@ninetailed/experience.js-utils';import { getHeroData } from'api/yourDataFetcher';consthero=awaitgetHeroData('aHeroEntryId')constmappedExperiences= (hero.ntExperiencesCollection?.items || []).map((experience) => {return { id:experience.ntExperienceId, name:experience.ntName, type:experience.ntType, config:experience.ntConfig,// This syntax accounts for the possibility of an audience not being set on an experiment...(experience.ntAudience? { audience: { id:experience.ntAudience.ntAudienceId// If mapping for the Preview Plugin, this displays audience names name: experience.ntAudience.ntName }, } } : {}) variants: experience.ntVariantsCollection.items.map((variant) => {return { id:variant.sys.id,// Required// Map any other fields required by your rendering component...variant } }) } }).filter((experience) =>ExperienceMapper.isExperienceEntry(experience)).map((experience) =>ExperienceMapper.mapExperience(experience));
Notice the use of fragments to capture the sys.id, since this is required on the Ninetailed Experience (NtExperience) entry as well as all variants referenced by the entry. Additionally, note the use of a fragment to isolate the fields of the Experience entry so that the base HeroEntry fragment can be used to query both the baseline and the variant content without introducing a circular reference.
Your exact query and mapping will vary depending on:
your content model,
the props required by the component you use to render your content, and
whether the Experiences you are mapping are attached to a modular block or a standalone entry
Note that because Ninetailed extends your content model with additional references, you'll need to fetch additional data from the Contentstack Delivery API to power this mapping.
Modular blocks require somewhat more complex mapping, because all experiences for all modular blocks on a page are stored in a top-level field of an entry. This mapping function demonstrates narrowing down this field to only experiences relevant to a single modular block, mapping its associated variants, and finally mapping to the format required by the <Experience> component.
import { ExperienceMapper } from'@ninetailed/experience.js-utils-contentful'import { createClient } from'contentful';constclient=createClient({ accessToken:'youtAccessToken', space:'yourSpaceId'})// Specify what entries with Ninetailed Experience references to get from Contentfulconstquery= {...}constrawEntries=awaitclient.getEntries(query);// Extract one entry, as an exampleconst [yourEntry] =rawEntries.items// Filter and map with ExperienceMapper methodsconstexperiences= (yourEntry.fields.nt_experiences || []).filter(ExperienceMapper.isExperienceEntry).map(ExperienceMapper.mapExperience)
Determines if a provided entry is of valid type to be consumed by mapExperience. Use with .filter to remove any invalidly typed experiences.
mapExperience
Transform an experience to the type required by the <Experience> component.
isExperimentEntry
Determines if a provided entry is of valid type to be consumed by mapExperiment. Use with .filter to remove any invalidly typed experiments.
mapExperiment
Transform an experiment to the type required by the React and Next.js <NinetailedProvider>experiments prop.
mapCustomExperience
[Contentful Library only] If you need to modify how the variants referenced by an experience entry retrieved from Contentful are mapped, use this method to pass a custom variant mapping function. Example usage:
constexperiences=myExperience.fields.nt_experiences.filter(ExperienceMapper.isExperienceEntry).map((experience) => {ExperienceMapper.mapCustomExperience(experience, (variant) => { id:variant.sys.id // required// Add any data required by your `component` prop on the <Experience> component...variant.fields, someComponentProp:variant.foo }); })
mapCustomExperienceAsync
[Contentful Library only, SDK >= 7.7.x] Similar to mapCustomExperience, but allows asynchronous operations to be executed in the variant mapping step.
constexperiences=myExperience.fields.nt_experiences.filter(ExperienceMapper.isExperienceEntry).map((experience) => {ExperienceMapper.mapCustomExperienceAsync(experience,async (variant) => {awaitnewPromise ((resolve) =>setTimeout(resolve,1000)); // Simulated delay supported by async handlerreturn { id:variant.sys.id // required// Add any data required by your `component` prop on the <Experience> component...variant.fields, someComponentProp:variant.foo } }); })
mapBaselineWithExperiences
[Contentful Library only] Supply an object representing a baseline entry and it's attached experiences and return an array of filtered and mapped experiences.