The Contentful plugin for Amplitude Experiment enables businesses to create variations of content in Contentful, and use Experiment to control which variant users see, and track performance of those variants.
To use the plugin, ensure you have the following:
https://app.amplitude.com/experiment/<ORG_URL>/dashboard
Complete the following steps in Contentful and Amplitude to add and activate the Contentful plugin for Amplitude Experiment.
When you enable the plugin, a Variant Container
content model appears on the Content Model tab.
The variant container in Contentful works with Amplitude Experiment to decide which variant of your experiment displays to each user.
For best results, Amplitude recommends you a Reference content type to hold experiments. In the Reference content type
To add a variant container:
Hero Banner
or Demo CTA
.After you configure the variant container and reference field, open the page on which you want to add an experiment.
Contentful returns JSON when a user requests the page.
1{ 2 "__typename": "PageLanding", 3 "sys": { 4 "id": "2cayfg7wVF5WezADCHgSgL", 5 "spaceId": "4y4crvvoco9a" 6 }, 7 "internalName": "Homepage", 8 "heroBanner": { 9 "__typename": "VariationContainer", 10 "experiment": { 11 "id": "183980", 12 "key": "contentful-hero-banner", 13 "name": "contentful-hero-banner", 14 "tags": [], 15 "state": "running", 16 "deleted": false, 17 "enabled": true, 18 "endDate": null, 19 "decision": null, 20 "variants": [ 21 { 22 "key": "control" 23 }, 24 { 25 "key": "treatment" 26 } 27 ], 28 "projectId": "289220", 29 "startDate": "2024-02-22", 30 "deployments": ["14"], 31 "description": "", 32 "bucketingKey": "amplitude_id", 33 "bucketingSalt": "28fWNw1M", 34 "bucketingUnit": "User", 35 "decisionReason": null, 36 "evaluationMode": "remote", 37 "experimentType": "hypothesis-testing", 38 "rolloutWeights": { 39 "control": 1, 40 "treatment": 1 41 }, 42 "targetSegments": [], 43 "stickyBucketing": false, 44 "rolledOutVariant": null, 45 "rolloutPercentage": 0 46 }, 47 "experimentId": "contentful-hero-banner", 48 "meta": { 49 "control": "6rmYK8YKYtTkKcFRY9Pf2w", 50 "treatment": "kwDjI2f2vKE2GvQoeqq1d" 51 }, 52 "variationsCollection": { 53 "items": [ 54 { 55 "__typename": "Hero", 56 "sys": { 57 "id": "6rmYK8YKYtTkKcFRY9Pf2w", 58 "spaceId": "4y4crvvoco9a" 59 }, 60 "preHeadline": "Organic Products", 61 "headline": "100% Fresh Food", 62 "cta": "Shop Now", 63 "description": "Whatever you want from your local stores, brought right to your door. \t\t\t\t\t\t\t", 64 "image": { 65 "__typename": "Asset", 66 "sys": { 67 "id": "6PkraSxWWd96AU6FTYVssd" 68 }, 69 "title": "Fresh food", 70 "description": "", 71 "width": 2560, 72 "height": 960, 73 "url": "https://images.ctfassets.net/4y4crvvoco9a/6PkraSxWWd96AU6FTYVssd/69b8d7f7cabb9f578097d50f2bf8aa70/Hero-3-1-scaled.jpg", 74 "contentType": "image/jpeg" 75 } 76 }, 77 { 78 "__typename": "Hero", 79 "sys": { 80 "id": "kwDjI2f2vKE2GvQoeqq1d", 81 "spaceId": "4y4crvvoco9a" 82 }, 83 "preHeadline": "Exclusive Offer", 84 "headline": "Loyalty Program", 85 "cta": "Get Free Shipping", 86 "description": "We missed you! Finish your order today and get free shipping when you join our loyalty program.", 87 "image": { 88 "__typename": "Asset", 89 "sys": { 90 "id": "6WFOK0460CwrW7aChl1QjM" 91 }, 92 "title": "Loyalty Green", 93 "description": "", 94 "width": 1920, 95 "height": 720, 96 "url": "https://images.ctfassets.net/4y4crvvoco9a/6WFOK0460CwrW7aChl1QjM/e52fb2129b848203f6006ff9881309d9/Hero-_-loyalty-green.jpg", 97 "contentType": "image/jpeg" 98 } 99 }100 ]101 }102 }103}
The experiment
object contains helpful metadata about the experiment. To render the front end and include the experiment, use the meta
and variationsCollection
objects. Amplitude Experiment delivers the variant identifier, and matches it to an option in the meta
object. After the variant is set, you can:
variationsCollection
object to show the variation with the matching ID.For more information, see the following React / Typescript example:
1import { Experiment } from '@amplitude/experiment-js-client'; 2 import sdk from 'contentful-sdk'; 3 4 5 export const experiment = Experiment.initialize(process.env.NEXT_PUBLIC_AMPLITUDE_EXPERIMENT_CLIENT_KEY || "", { 6 debug: true, 7 }); 8 const [hero, setHero] = useState<Hero | null>(null); 9 useEffect(() => {10 const matchExperimentData = async () => {11 await experiment.fetch({12 user_id: userId,13 });14 const heroBanner = sdk.getEntry('ENTRY_ID_HERE');15 const variant = experiment.variant(heroBanner?.experimentId ?? 'control');16 let resolvedVariant;17 if (heroBanner && variant.value) {18 const variation = heroBanner.meta[variant.value];19 resolvedVariant = heroBanner.variationsCollection?.items.find(hero => {20 return hero?.__typename === 'Hero' && hero?.sys.id === variation;21 });22 setHero(resolvedVariant);23 }24 };25 matchExperimentData();26 }, [heroBanner, userId]);
Be sure to account for cases where users receive off
as the value that experiment.variant()
returns.
Thanks for your feedback!
June 16th, 2024
Need help? Contact Support
Visit Amplitude.com
Have a look at the Amplitude Blog
Learn more at Amplitude Academy
© 2024 Amplitude, Inc. All rights reserved. Amplitude is a registered trademark of Amplitude, Inc.