Skip to content

jondjones-poc/optimizely-fullstack-contentful-gatsby

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Contentful, Gatsby, Optimizely Demo - By Jon D Jones 💥

Use Optimizely Full-stacks power to display two variations on the homepage. This project has three examples of how to use Optimizely FullStack:

  • Hello World & Automate-with-webhooks pages: Contentful content and Optimizely SDK Integration

  • Homepage: Use the SDK to talk to Optimizely to get experiment data

  • Homepage: Use the SDK to talk to Optimizely to get feature flag data

This project makes uses of @optimizely/react-sdk version 1.1.0. You have to use this version of the SDK as this is the last update that uses the V3 version of the Optimizely SDk. This version is currently the only supported ContentFul to Optimizely integration option.

👻 Live Site URL & Status 👺

https://contentful-gatsby-optimizely.netlify.app/

Netlify Status

Set-Up 😍

Create an .env file and add these properties:

CONTENTFUL_ACCESS_TOKEN=*****
CONTENTFUL_SPACE_ID=*****
SDK_KEY=*****
GATSBY_SDK_KEY=*****

Architecture 😍

Image

Data File Management 📊

The data file is fetched within gatsby-browser.js and then passed down into the app. This approach ensures there is no latency on the web page waiting for the file to load. More information can be found on can be found on gatsby-browser.js here.

const optimizelyDataFileReact = reactOptimizelySDK.createInstance({
  sdkKey: process.env.GATSBY_SDK_KEY,
  isServerSide: true 
})

NOTE: Need to pass a prop called isServerSide if calling on server-side

Another way of potentially managing global state is the Global Context Plugin.

GraphQL 👌

Below shows an example GraphQL query to get the variation from optimizely from Contentful. You need to use meta to map the experiment id to the contentful id. You can then access the content from contentful within variation_container:

 author {
    ... on ContentfulVariationContainer {
        id
        experimentId
        experimentKey
        meta {
        id
        internal {
            content
            description
            ignoreType
            mediaType
        }
        }
        variations {
        id
        name
        contentful_id
        variation_container {
            id
        }
        }
        experimentTitle
    }
}

See templatesblog-post.js

How To Add Variations In Contentful 🍲

ContentFul

Contentful Setup

FullStack

Contentful Setup

Events 📅

It is also possible to pass data back to Optimizely in form of events. The code to do events can be seen below:

const onClick = () => {
    console.log("click");
    optimizelyDataFile.track('purchase')
}

This feature can be seen in pages -> index.js:

how-does-the-optimizely-web-bring-your-own-ID-work-1.gif

Troubleshooting Tips 💁

  • Always provide a user ID, other you will get an error!

  • You need the data-file before you run experiments. In templatesblog-post.js the data file is retrieved on page request. This is sub-optimal as it introduces a delay. To make an async request wrap the call like this:

optimizely.onReady().then(() => {
}
  • Use functional components rather than Component based components !!!

  • See Console.log() for data structures 🔥🔥🔥

Useful Links ⚡️

About

Example fo how to build a website using Gatsby, Contentful and Optimizely Fullstack

Topics

Resources

Stars

Watchers

Forks

Contributors 2

  •  
  •