Edit in GitHubLog an issue

Downloading, Storing, and Updating the Rule Artifact Automatically via the Adobe Target SDK

This approach is best when you are able to initialize the Adobe Target SDK at the same time you initialize and start your web server. The rule artifact will be downloaded by the Adobe Target SDK and cached into memory before your web server application starts serving requests. Once your web application is up and running, all Adobe Target decisions will be executed using the in-memory rule artifact. The cached rule artifact will be updated based on the pollingInterval you specify during the SDK initialization step.

Summary of steps#

  1. Install the SDK
  2. Initialize the SDK
  3. Store and Use the Rule Artifact

Install the SDK#

Copied to your clipboard
npm i @adobe/target-nodejs-sdk -P

Initialize the SDK#

  1. First, import the SDK. Import to the same file from which you can control your server start-up.
Copied to your clipboard
const TargetClient = require("@adobe/target-nodejs-sdk");
  1. To configure the SDK, use the create method.
Copied to your clipboard
1const CONFIG = {
2 client: "<your target client code>",
3 organizationId: "your EC org id",
4 decisioningMethod: "on-device",
5 pollingInterval : 300000,
6 events: {
7 clientReady: startWebServer
8 }
9};
10
11const TargetClient = TargetClient.create(CONFIG);
12
13function startWebServer() {
14 //Adobe Target SDK has now downloaded the JSON Artifacts and is available in the memory.
15 //You can start your web server now to serve requests now.
16}
  1. Both client and organizationId can be retrieved from Adobe Target by navigating to Administration > Implementation, as shown here.

alt image

Store and Use the Rule Artifact#

You do not need to manage the rule artifact yourself and calling the SDK methods should be straightforward.

Copied to your clipboard
1//req is the request object from the server request listener method
2const targetCookie = req.cookies[TargetClient.TargetCookieName];
3const request = {
4 context: {
5 channel: "web"
6 },
7 execute: {
8 mboxes: [{
9 address: { url: req.headers.host + req.originalUrl },
10 name: "on-device-homepage-banner"
11 }],
12 },
13};
14
15TargetClient.getOffers({
16 request,
17 targetCookie
18}).then(function(response) {
19 //This Target response is coming from the In-memory Target artifact.
20 console.log("Target response", response);
21}).catch(function(err) {
22 console.error("Target:", err);
23})

Here is an example that starts a web application after initializing the Adobe Target SDK.

Copied to your clipboard
1const express = require("express");
2const cookieParser = require("cookie-parser");
3const TargetClient = require("@adobe/target-nodejs-sdk");
4const CONFIG = {
5 client: "<your target client code>",
6 organizationId: "your EC org id",
7 decisioningMethod: "on-device",
8 pollingInterval : 300000,
9 events: {
10 clientReady: startWebServer
11 }
12};
13
14const app = express();
15const targetClient = TargetClient.create(CONFIG);
16
17app.use(cookieParser());
18
19function saveCookie(res, cookie) {
20 if (!cookie) {
21 return;
22 }
23
24 res.cookie(cookie.name, cookie.value, {maxAge: cookie.maxAge * 1000});
25}
26
27const getResponseHeaders = () => ({
28 "Content-Type": "text/html",
29 "Expires": new Date().toUTCString()
30});
31
32function sendSuccessResponse(res, response) {
33 res.set(getResponseHeaders());
34 saveCookie(res, response.targetCookie);
35 res.status(200).send(response);
36}
37
38function sendErrorResponse(res, error) {
39 res.set(getResponseHeaders());
40 res.status(500).send(error);
41}
42
43function startWebServer() {
44 app.get('/*', async (req, res) => {
45 const targetCookie = req.cookies[TargetClient.TargetCookieName];
46 const request = {
47 execute: {
48 mboxes: [{
49 address: { url: req.headers.host + req.originalUrl },
50 name: "on-device-homepage-banner" // Ensure that you have a LIVE Activity running on this location
51 }]
52 }};
53
54 try {
55 const response = await targetClient.getOffers({ request, targetCookie });
56 sendSuccessResponse(res, response);
57 } catch (error) {
58 console.error("Target:", error);
59 sendErrorResponse(res, error);
60 }
61 });
62
63 app.listen(3000, function () {
64 console.log("Listening on port 3000 and watching!");
65 });
66}
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.