This is a Next.js example built with App Router. The same scenario can be achieved with any front-end application framework supported by Uniform.
- Uniform account with an empty project.
- Twilio Engage account with the ability to retrieve connection settings.
-
Setup Uniform project environment variables:
UNIFORM_API_KEY= UNIFORM_PROJECT_ID=
-
Setup Segment environment variables:
NEXT_PUBLIC_ANALYTICS_WRITE_KEY= SEGMENT_SPACE_ID= SEGMENT_API_KEY=
-
Install dependencies with
npm install -
Change the API key and Project ID env vars in
.envwith your own.Make sure your API key has "Developer" role to be able to push content.
-
npm run pushandnpm run publishto push content from disk (/uniform-datafolder) to your project. -
Run the development server:
npm run dev
Open http://localhost:3000 with your browser to see the result.
- The Segment script registered in
app/layout.tsxallows to send data to Segment. - API route to fetch traits from Segment Profile API, see
/app/api/traits/route.ts. - Front-end component that sets Uniform tracker context client-side:
components/tracker-score-sync/index.tsx
There are multiple options as it comes to fetching Segment data. The default example is using client-side fetch, so you don't block page rendering. You can also do it the blocking way, see /app/[[...path]]/page.tsx.ssr as an example of the server-side blocking approach. Naturally, this approach is not compatible with the Static Site Generation mode.
The tradeoff of this approach is that the initial page render will be dependent on the Segment Profile API response, but you get fresh visitor data during page load. If you go with this approach, remove the <TrackerScoreSync /> component.