Web Usage
Embed CredibleMind components in a website, from a quickstart to going live.
Quickstart
This shows the CredibleMind topics browser for the demo client benovo.
Load the embed script
<head>
<script src="https://widget.crediblemind.com/embed.js"></script>
</head>Add a container and render
<body>
<div id="cm-embed-container"></div>
<script>
CMEmbed.PageComponent({
client: 'benovo',
env: 'demo',
parent: window.location,
components: ['topics']
}).render('#cm-embed-container')
</script>
</body>Verify it works
Open the page: the topics browser loads inside it. Then add ?cm_page=topics/anxiety to the page URL and reload. The Anxiety topic should open, which confirms parent is reading the URL.
This works, but the iframe is a fixed 1000px tall and the parent URL does not follow navigation. The recommended setup below fixes both.
Which script?
Load embed.js, as shown on this page, on live pages. The env property, not the script name, decides whether the demo or production site is shown.
Previewing the new experience
To try the upcoming CredibleMind experience on a test page, load https://widget.crediblemind.com/embed-v2-dev.js instead and set env: 'demo'. It takes the same properties and shows the preview site, embedded.crediblemind.com. Keep embed.js on live pages.
Recommended setup
Place the configuration just before </body>. Replace YOUR_CLIENT_IDENTIFIER with the identifier from your client success representative.
<script>
const containerId = 'cm-embed-container'
CMEmbed.PageComponent({
client: 'YOUR_CLIENT_IDENTIFIER',
env: 'demo', // change to 'production' when you go live
parent: window.location,
type: 'bare',
components: ['topics'], // shown when the URL has no cm_page parameter
// Keep cm_page in your URL in sync as the user navigates.
onRouteChange: route => {
const page = route.length > 1 ? '?cm_page=' + route : window.location.pathname
history.replaceState(null, null, page)
if (!route) window.location.reload()
const container = document.getElementById(containerId)
if (container) {
window.scrollTo({
top: window.pageYOffset + container.getBoundingClientRect().top,
behavior: 'smooth'
})
}
},
// Resize the iframe to fit its content instead of a fixed 1000px.
setIframeHeight: height => {
const container = document.getElementById(containerId)
if (container) {
container.firstElementChild.style.height = (height || 200) + 'px'
}
},
// Position modals and scrolling relative to your page.
onModalOpen: () => ({ top: window.scrollY, left: window.scrollX }),
onScrollTo: position => window.scrollTo({ top: position, behavior: 'smooth' }),
styles: { backgroundColor: '#f7f7f7' },
controls: { hideHeader: true }
}).render('#' + containerId)
</script>Every property is described in the Properties Glossary. Usually you only change client, env, and the optional styles, controls and components.
Choosing the page to show
There are two ways to set the CredibleMind page, and the URL wins if both are set:
- URL parameter. Add
cm_pageto your container page URL:https://www.example.com/wellbeing?cm_page=topics. This needsparent: window.location. componentsproperty. Set the default page in JavaScript:components: ['topics']. Only the first item is used.
Only one component can be shown per page. To switch components, link to the container page with a different cm_page.
Other query parameters on the container page
If the container page URL has other query parameters but no cm_page (for example ?utm_source=email), the embed currently treats them as the page to load and shows an error page instead of components. When you link to the container page with tracking parameters, include cm_page as well, for example ?cm_page=topics&utm_source=email.
| Page | cm_page value | components value |
|---|---|---|
| All topics, with search | topics | ['topics'] |
| One topic | topics/{topic}, for example topics/anxiety | ['topics/anxiety'] |
| All assessments | assessments | ['assessments'] |
| All articles | blogs (news and insights open the same page) | ['blogs'] |
| All lists | lists | ['lists'] |
| One list | lists/{list}, for example lists/family-and-boundaries (see List Options) | ['lists/family-and-boundaries'] |
| Disaster resources | landing/disaster-related-resources | ['landing/disaster-related-resources'] |
To test, open your container page with ?cm_page=topics. It should look like this demo.
Going live
- Tell your client success representative you are ready, and send them the URL of your container page. CredibleMind then redirects visitors who open your CredibleMind site directly to your container page with the right
cm_page, so content is only seen embedded. (This does not apply withredirect: 'new_tab'.) - Change
envfrom'demo'to'production'.demoshowsdevelopment.crediblemind.com;productionshowshttps://{client}.crediblemind.com. - Check
clientis your production client identifier. If one page serves several clients, insert the right identifier into the script before it runs; nothing else needs to change.