Skip to main content

Causal Snippet API Contract

optOut

Call this method when the user explicitly opts out of cookie tracking.

window.causalLabs.optOut()

optIn

Call this method when the user explicitly opts into cookie tracking.

window.causalLabs.optIn()

defaultOptOut

By default Causal defaults to opting users into cookie tracking consent. Call this method to disable this behavior and force users to explicitly accept or decline cookie tracking prior to any causal personalization can be performed. When using this behavior please ensure that the optIn method is called when the the user selects their tracking preference to enable Causal personalization on the next page load.

Note: this should be done in head tag where the Causal snippet is included. Please see the example below:

<script>
!function(){for(let a of((w=window).causalLabs=w.causalLabs||{},w.causalLabs._operations=[],["optOut","optIn","defaultOptOut","identifyUser","noInitialRender","renderNow"]))w.causalLabs[a]=function(){w.causalLabs._operations.push({name:a,args:arguments})}}();
</script>
<script>
window.causalLabs.defaultOptOut();
</script>
<script src="https://example_server.com/iserver/copyTesting" id="CausalLabsScript"></script>

identifyUser

In some instances it may be preferable to use your user ids with the Causal integration. Calling this method will force the snippet to use the supplied user id for this user instead of generating one automatically.

Note: this should be done in head tag where the Causal snippet is included. Please see the example below:

<script>
!function(){for(let a of((w=window).causalLabs=w.causalLabs||{},w.causalLabs._operations=[],["optOut","optIn","defaultOptOut","identifyUser","noInitialRender","renderNow"]))w.causalLabs[a]=function(){w.causalLabs._operations.push({name:a,args:arguments})}}();
</script>
<script>
window.causalLabs.identifyUser("example_user_id");
</script>
<script src="https://example_server.com/iserver/copyTesting" id="CausalLabsScript"></script>

noInitialRender

When integrating the Causal snippet on a website powered by React you will need to tell the Causal snippet not to perform any personalizations until after the react render cycle is complete. It will be necessary to then call the renderNow when the render cycle is complete.

Note: this should be done in head tag where the Causal snippet is included. Please see the example below:

<script>
!function(){for(let a of((w=window).causalLabs=w.causalLabs||{},w.causalLabs._operations=[],["optOut","optIn","defaultOptOut","identifyUser","noInitialRender","renderNow"]))w.causalLabs[a]=function(){w.causalLabs._operations.push({name:a,args:arguments})}}();
</script>
<script>
window.causalLabs.noInitialRender();
</script>
<script src="https://example_server.com/iserver/copyTesting" id="CausalLabsScript"></script>

renderNow

This will cause the Causal snippet to perform any page personalizations.

Note: This should only be used if you are integrating Causal onto a website powered by React and are using the noInitialRender method as well.

window.causalLabs.renderNow()

An example integration could be done using a custom provider that will call renderNow via a useEffect when necessary.

export function CausalSnippetProvider({
children,
}: {
children: React.ReactNode;
}) {
const href = typeof window == "undefined" ? "" : window.location.href;

useEffect(() => {
// manually trigger Causal update after render/hydration
(window as any).causalLabs.renderNow();
}, [href]);

return <>{children}</>;
}

Example usage of the provider in your app file.

export default function MyApp({ Component, pageProps }: AppProps) {
return (
<CausalSnippetProvider>
<AppComponent />
</CausalSnippetProvider>
);