Uniform plugin for Sanity
This plugin augments Sanity Studio with ability to assign personalization criteria and enrichments tags to any documents inside your Sanity dataset.
By Andrew Kumar
Install command
npm i sanity-plugin-uniformSanity Studio with Uniform plugin
Bring powerful edge-side personalization into Sanity
This Sanity Studio is based on v3 and is wired up with the Uniform plugin that is compatible with the latest Uniform Context.
Prerequisites
- Uniform project created (start for free at https://uniform.app)
- Sanity.io project.
Getting started
npm install- Create
.env.development(for local dev) or.env.production(for prod deployment), depending on your needs.See
.env.exampleas a reference - Fill out your env vars (for Sanity and Uniform).
npm start.- Configure project id and dataset settings in
sanity.cli.ts
Import sample content into your dataset (optional)
- Install sanity CLI with
npm install -g @sanity/cli - Login to Sanity.io with
sanity loginto be able to run some admin commands later - Import content structure and content into your Sanity dataset:
sanity dataset import ./content-export.ndjson <DATASET_NAME>
How is the plugin installed?
Open
sanity.config.tsand import Uniform plugin:import {uniformPlugin} from '@uniformdev/sanity-plugin-uniform'Add Uniform plugin with configuration sourcing values from your environment variables:
plugins: [ uniformPlugin({ apiHost: import.meta.env.SANITY_STUDIO_UNIFORM_HOST || 'https://uniform.app', apiKey: import.meta.env.SANITY_STUDIO_UNIFORM_API_KEY!, projectId: import.meta.env.SANITY_STUDIO_UNIFORM_PROJECT_ID!, }), ],
Sample schema
The sample schema is included in schemas/uniform but you don't have to use it.
There are two sides of Uniform Context - personalization criteria that configure criteria when a particular piece of content is supposed to be shown, and enrichment tags - the ability to decorate your content (typically, pages) with Uniform Enrichments, allowing to collect behavior and personalize on it.
If you are not using the sample schema, you will need to at least add one field of type uniform.personalizationCriteria to any content type in order to personalize a given content type (see schemas/uniform/hero.ts for reference):
{
name: 'personalizationCriteria',
type: 'uniform.personalizationCriteria',
title: 'Personalization Criteria',
}Optionally, you can also add enrichments to any content type, which allows to collect behavioral information when such content is visited during a visitor session.
To use this feature, you will need to add at least one field of type uniform.enrichmenttags to any content type in order to personalize a given content type (see schemas/uniform/page.ts for reference):
{
name: 'enrichmentTag',
type: 'uniform.enrichmenttags',
title: 'Enrichments',
}About Environment Variables
The .env files must be suffixed with either .development or .production for Sanity to use them. The environment suffix is determined by the following:
process.env.SANITY_ACTIVE_ENVtakes precedenceprocess.env.NODE_ENVis fallback- if neither of the above are defined, then
developmentis using forsanity startandproductionis used forsanity buildandsanity deploy.