Contentstack integration for Lytics lio-client - enrich CMS content with Lytics analytics
npm install @lytics/lio-client-contentstackContentstack CMS integration plugin for @lytics/lio-client.
Monitors Contentstack sync workflows and enriches CMS entries with Lytics analytics (topics, engagement, segments).
``bash`Requires core client
npm install @lytics/lio-client @lytics/lio-client-contentstack
`typescript
import { createLioClient } from '@lytics/lio-client';
import { contentstackPlugin } from '@lytics/lio-client-contentstack';
const lio = createLioClient({
apiKey: process.env.LYTICS_API_KEY,
plugins: [contentstackPlugin]
});
await lio.init();
// Check sync status
const status = await lio.contentstack.getSyncStatus();
console.log(Last sync: ${status.lastSync});Entries synced: ${status.entriesSynced}
console.log();
// Enrich a Contentstack entry
const entry = await csStack.entry('blt123').fetch();
const enriched = await lio.contentstack.enrich(entry);
console.log(enriched._lytics.topics);
// Batch enrichment
const entries = await csStack.contentType('blog_post').query().find();
const enrichedList = await lio.contentstack.enrichMany(entries.entries);
// Scan all Contentstack content in Lytics
for await (const content of lio.contentstack.scanContent()) {
console.log(content.url, content.lytics);
}
// Get analytics
const analytics = await lio.contentstack.getAnalytics();
console.log(Total entries: ${analytics.totalEntries});Top topics:
console.log(, analytics.topTopics);`
Returns: Promise
`typescript`
{
status: 'sleeping' | 'running' | 'completed' | 'failed' | 'not_configured',
lastSync: string | null,
entriesSynced: number,
contentTypes: string[],
workflowId?: string
}
Parameters:
- entryOrUrl: Contentstack entry object or URL string
Returns: Promise
Parameters:
- options: Optional scan options (filter, limit, fields)
Returns: AsyncGenerator
Returns: Promise
`typescript`
{
totalEntries: number,
topTopics: Array<{ topic: string; count: number }>,
contentTypes: Record
}
Parameters:
- entry: Contentstack entry object with url or href field
Returns: Promise
The _lytics field contains:`typescript`
{
topics?: Record
hashedurl?: string,
segments?: string[],
url?: string
}
Parameters:
- entries: Array of Contentstack entry objects
Returns: Promise
1. Matches Contentstack entries to Lytics content by URL
2. Adds _lytics field with topics, hashedurl, segments/v2/content/entity
3. Uses for single lookups/api/segment/scan
4. Uses for batch operationscontentstack-import
5. Monitors workflow for sync status
Customize the plugin behavior:
`typescript`
const lio = createLioClient({
apiKey: process.env.LYTICS_API_KEY,
plugins: [contentstackPlugin],
contentstack: {
workflowName: 'contentstack-import', // Workflow to monitor
streamName: 'contentstack', // Stream name in Lytics
},
});
typescript
function DashboardWidget() {
const [status, setStatus] = useState(null); useEffect(() => {
async function fetchStatus() {
const sync = await lio.contentstack.getSyncStatus();
setStatus(sync);
}
fetchStatus();
}, []);
return
Last sync: {status?.lastSync};
}
`$3
`typescript
function EntrySidebarWidget({ entry }) {
const [enrichment, setEnrichment] = useState(null); useEffect(() => {
async function fetchEnrichment() {
const enriched = await lio.contentstack.enrich(entry);
setEnrichment(enriched._lytics);
}
fetchEnrichment();
}, [entry]);
return (
Topics
{enrichment?.topics && Object.keys(enrichment.topics).map(topic => (
{topic}
))}
);
}
`$3
`typescript
const analytics = await lio.contentstack.getAnalytics();console.log(
Total synced entries: ${analytics.totalEntries});
console.log('Top topics:');
analytics.topTopics.forEach(({ topic, count }) => {
console.log( ${topic}: ${count} entries);
});
``MIT