AAM Segments

Adobe Audience Manager segments can be leveraged via Adobe Target SDKs. In order to leverage AAM segments, the following fields need to be provided:

AAM segments are not supported for on-device decisioning activities.

Field Name

Required

Description

locationHint

Yes

DCS Location Hint is used to determine which AAM DCS Endpoint to hit in order to retrieve the profile. Must be >= 1.

marketingCloudVisitorId

Yes

Marketing Cloud Visitor ID

blob

Yes

AAM Blob is used to send additional data to AAM. Must not be blank and the size <= 1024.

The SDK will automatically populate these fields for you when making a getOffers method call, but you will need to ensure a valid visitor cookie is provided. In order to get this cookie, you need to implement VisitorAPI.js in the browser.

Implementation Guide

Use of Cookies

Cookies are used to correlate Adobe Audience Manager requests with Adobe Target requests. These are the cookies used in this implementation.

cookie

name

Description

visitor cookie

AMCVS_XXXXXXXXXXXXXXXXXXXXXXXX%40AdobeOrg

This cookie is set by VisitorAPI.js when it is initialized with visitorState from the target getOffers response.

target cookie

mbox

Your web server must set this cookie using the name and value of targetCookie from the target getOffers response.

Overview of steps

Suppose a user enters a URL into a browser which sends a request to your web server. When fulfilling that request...

  1. The server reads the visitor and target cookies from the request.

  2. The server makes a call to the getOffers method of the Target SDK, specifying the visitor and target cookies if available.

  3. When the getOffers call is fulfilled, values for targetCookie and visitorState from the response are used.

    1. A cookie is set on the response with values taken from targetCookie. This is done using the Set-Cookie response header, which tells the browser to persist the target cookie.

    2. An HTML response is prepared that initializes VisitorAPI.js and passes in visitorState from the target response.

  4. The HTML response is loaded in the browser...

    1. VisitorAPI.js is included in the document header.

    2. VisitorAPI is initialized with visitorState from the getOffers SDK response. This will cause the visitor cookie to be set in the browser so it will be sent to the server on subsequent requests.

Example code

The following code sample implements each of the steps outlined above. Each step appears in the code as an inline comment next to its implementation.

Node.js SDK
Java SDK
Node.js SDK

This sample relies on express, a Node.js web framework.

server.js

const fs = require("fs");
const express = require("express");
const cookieParser = require("cookie-parser");
const Handlebars = require("handlebars");
const TargetClient = require("@adobe/target-nodejs-sdk");
const CONFIG = {
client: "acmeclient",
organizationId: "[email protected]",
timeout: 10000,
logger: console,
};
const targetClient = TargetClient.create(CONFIG);
const TEMPLATE = fs.readFileSync(`${__dirname}/index.handlebars`).toString();
const handlebarsTemplate = Handlebars.compile(TEMPLATE);
Handlebars.registerHelper("toJSON", function (object) {
return new Handlebars.SafeString(JSON.stringify(object, null, 4));
});
const app = express();
app.use(cookieParser());
app.use(express.static(__dirname + "/public"));
app.get("/", async (req, res) => {
// The server reads the visitor and target cookies from the request.
const visitorCookie =
req.cookies[
encodeURIComponent(
TargetClient.getVisitorCookieName(CONFIG.organizationId)
)
];
const targetCookie = req.cookies[TargetClient.TargetCookieName];
const address = { url: req.headers.host + req.originalUrl };
const targetRequest = {
execute: {
mboxes: [
{ name: "homepage", index: 1, address },
{ name: "SummerShoesOffer", index: 2, address },
{ name: "SummerDressOffer", index: 3, address }
],
},
};
res.set({
"Content-Type": "text/html",
Expires: new Date().toUTCString(),
});
try {
// The server makes a call to the `getOffers` method of the Target SDK specifying the visitor and target cookies if available.
const targetResponse = await targetClient.getOffers({
request: targetRequest,
visitorCookie,
targetCookie,
});
// When the `getOffers` call is fulfilled, values for `targetCookie` and `visitorState` from the response are used.
// A cookie is set on the response with values taken from `targetCookie`. This is done using the `Set-Cookie` response header which tells the browser to persist the target cookie.
res.cookie(
targetResponse.targetCookie.name,
targetResponse.targetCookie.value,
{ maxAge: targetResponse.targetCookie.maxAge * 1000 }
);
// An HTML response is prepared that initializes VisitorAPI.js and passes in `visitorState` from the target response.
const html = handlebarsTemplate({
organizationId: CONFIG.organizationId,
targetResponse,
});
res.status(200).send(html);
} catch (error) {
console.error("Target:", error);
res.status(500).send(error);
}
});
app.listen(3000, function () {
console.log("Listening on port 3000 and watching!");
});

index.handlebars

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>ECID (Visitor API) Integration Sample</title>
<!-- VisitorAPI.js is included in the document header. -->
<script src="VisitorAPI.js"></script>
<script>
// VisitorAPI is initialized with visitorState from the `getOffers` SDK response. This will cause the visitor cookie to be set in the browser so it will be sent to the server on subsequent requests.
Visitor.getInstance("{{organizationId}}", {serverState: {{toJSON targetResponse.visitorState}} });
</script>
</head>
<body>
<h1>response</h1>
<pre>{{toJSON targetResponse}}</pre>
</body>
</html>
Java SDK

This sample uses spring, a Java web framework.

ClientSampleApplication.java

@SpringBootApplication
public class ClientSampleApplication {
public static void main(String[] args) {
System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG");
SpringApplication.run(ClientSampleApplication.class, args);
}
@Bean
TargetClient marketingCloudClient() {
ClientConfig clientConfig = ClientConfig.builder()
.client("acmeclient")
.organizationId("[email protected]")
.defaultDecisioningMethod(DecisioningMethod.SERVER_SIDE)
.build();
return TargetClient.create(clientConfig);
}
}

TargetController.java

@Controller
@RequestMapping("/")
public class TargetController {
@Autowired
private TargetClientService targetClientService;
@GetMapping
public String index(Model model, HttpServletRequest request, HttpServletResponse response) {
// The server reads the visitor and target cookies from the request.
List<TargetCookie> targetCookies = getTargetCookies(request.getCookies());
Address address = getAddress(request);
List<MboxRequest> mboxRequests = new ArrayList<>();
mboxRequests.add((MboxRequest) new MboxRequest().name("homepage").index(1).address(address));
mboxRequests.add((MboxRequest) new MboxRequest().name("SummerShoesOffer").index(2).address(address));
mboxRequests.add((MboxRequest) new MboxRequest().name("SummerDressOffer").index(3).address(address));
TargetDeliveryResponse targetDeliveryResponse = targetClientService.getOffers(mboxRequests, targetCookies, request,
response);
// An HTML response is prepared that initializes VisitorAPI.js and passes in `visitorState` from the target response.
model.addAttribute("visitorState", targetDeliveryResponse.getVisitorState());
model.addAttribute("targetResponse", targetDeliveryResponse);
model.addAttribute("organizationId", "[email protected]");
return "index";
}
}

TargetClientService.java

@Service
public class TargetClientService {
private final TargetClient targetJavaClient;
public TargetClientService(TargetClient targetJavaClient) {
this.targetJavaClient = targetJavaClient;
}
public TargetDeliveryResponse getOffers(List<MboxRequest> executeMboxes, List<TargetCookie> cookies, HttpServletRequest request, HttpServletResponse response) {
Context context = getContext(request);
ExecuteRequest executeRequest = new ExecuteRequest();
executeRequest.setMboxes(executeMboxes);
TargetDeliveryRequest targetDeliveryRequest = TargetDeliveryRequest.builder()
.context(context)
.execute(executeRequest)
.cookies(cookies)
.build();
// The server makes a call to the `getOffers` method of the Target SDK specifying the visitor and target cookies if available.
TargetDeliveryResponse targetResponse = targetJavaClient.getOffers(targetDeliveryRequest);
// When the `getOffers` call is fulfilled, values for `targetCookie` and `visitorState` from the response are used.
// A cookie is set on the response with values taken from `targetCookie`. This is done using the `Set-Cookie` response header which tells the browser to persist the target cookie.
setCookies(targetResponse.getCookies(), response);
return targetResponse;
}
}

TargetRequestUtils.java

index.html

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Target Only : GetOffer</title>
<!-- VisitorAPI.js is included in the document header. -->
<script src="../../js/VisitorAPI.js"></script>
<script th:inline="javascript">
// VisitorAPI is initialized with visitorState from the `getOffers` SDK response. This will cause the visitor cookie to be set in the browser so it will be sent to the server on subsequent requests.
Visitor.getInstance(/*[[${organizationId}]]*/ "", {serverState: /*[[${visitorState}]]*/ {}});
</script>
</head>
<body>
<h1>response</h1>
<pre>[[${targetResponse}]]</pre>
</body>
</html>