Disclaimer: This website requires Please enable JavaScript in your browser settings for the best experience.

The availability of features may depend on your plan type. Contact your Customer Success Manager if you have any questions.

Dev GuideAPI Reference
Dev GuideAPI ReferenceUser GuideGitHubDev CommunityOptimizely AcademySubmit a ticketLog In
API Reference

DCP

Describes Dynamic Customer Profile (DCP) API functions

You can use the dcp APIs if both of these conditions are true:

If either of the above doesn't apply, then window.optimizely.get('dcp') will return undefined.

Syntax

dcp = window["optimizely"].get("dcp");

Parameters

Parameter and Type

Child Attribute

Description

dcp string

N/A

Required.

Return value

Parameter and Type

Child Attribute

Description

DCPObject

N/A

getAttributeValue
functionh of these conditio

Child attribute of type DCPObject

This API function returns the visitor's value for a content-enabled profile attribute.

waitForAttributeValue
function these conditions are t

Child attribute of type DCPObject

This API function returns a Promise that will be resolved as soon as Optimizely Web Experimentation receives the visitor's value for a content-enabled profile attribute.

Example Call

dcp = window["optimizely"].get("dcp");

Example Return Value

dcp.getAttributeValue(attribute);
dcp.waitForAttributeValue(attribute);

getAttributeValue()

This API function returns the visitor's value for a content-enabled profile attribute.

Syntax

dcp.getAttributeValue(datasourceId, attributeId, attributeName);

Parameters

Parameter and Type

Child Attribute

Description

datasourceId integer

N/A

Required.

attributeId
integer

N/A

Required if attributeName is not provided.

attributeName
string

N/A

Required if attributeId is not provided. Does not work if descriptive names are masked in the Optimizely Web Experimentation snippet.

Return value

Parameter and Type

Child Attribute

Description

number or Boolean or string or Error

N/A

The uploaded attribute value, or undefined if any of the following are true:

  • A customer profile still needs to be uploaded for the current user.
  • A profile has been uploaded but doesn't include the desired attribute.
    The Optimizely Web Experimentation snippet is waiting for fresh data from the DCP service.\

Note: this function throws an error if any of the following are true:

  • The specified attribute does not exist.
  • The attribute is no content-enabled.
  • The attribute is specified by name even though names are masked in the Optimizely Web Experimentation snippet.

Example Call

dcp.getAttributeValue(datasourceId, attributeId, attributeName);

Examples

Specifying an attribute by ID

Get the value for a customer profile attribute.

Example code for specifying an attribute by ID

var dcp = window.optimizely.get('dcp');
var attributeValue = dcp.getAttributeValue({
  datasourceId: 123,
  attributeId: 456
});

Specifying an attribute by name

Get the value for a customer profile attribute.

Example code for specifying an attribute by name

var dcp = window.optimizely.get('dcp');
var attributeValue = dcp.getAttributeValue({
  datasourceId: 123,
  attributeName: 'Preferred Locale'
});

waitForAttributeValue()

This API function returns a Promise that will be resolved as soon as Optimizely Web Experimentation receives the visitor's value for a content-enabled profile attribute.

Syntax

dcp.waitForAttributeValue(datasourceId, attributeId, attributeName);

Parameters

Parameter and Type

Child Attribute

Description

datasourceId integer

N/A

Required.

attributeId
string

N/A

Required if attributeName isn't provided.

attributeName
string

N/A

Required if attributeId is not provided. Does not work if descriptive names are masked in the Optimizely Web Experimentation snippet.

Return value

Parameter and Type

Child Attribute

Description

Promise or Error

N/A

An ES6-style Promise that will be resolved with the uploaded attribute value, or with undefined if any of the following are true:
A customer profile still needs to be uploaded for the current user.
A profile has been uploaded, but it does not include the desired attribute.

If your campaign does not target a DCP Audience, then it's possible that profile data won't be fetched and the Promise will not be resolved.

Note that this function throws an error if any of the following are true:
The specified attribute doesn't exist.
The attribute isn't content-enabled.
The attribute is specified by name even though names are masked in the Optimizely Web Experimentation snippet.

Example Call

dcp.waitForAttributeValue(datasourceId, attributeId, attributeName);

Examples

Specifying an attribute by ID

Receive the value for a customer profile attribute once it has been fetched from the DCP server.

Example code for specifying an attribute by ID

var dcp = window.optimizely.get('dcp');
dcp.waitForAttributeValue({
  datasourceId: 123,
  attributeId: 456
}).then(
  function(attributeValue) {
    ...
  }
);

Specifying an attribute by name

Receive the value for a customer profile attribute once it has been fetched from the DCP server.

Example code for specifying an attribute by name

var dcp = window.optimizely.get('dcp');
dcp.waitForAttributeValue({
  datasourceId: 123,
  attributeName: 'Preferred Locale'
}).then(
  function(attributeValue) {
    ...
  }
);