Edit in GitHubLog an issue

Downloading, Storing, and Updating the Rule Artifact via JSON Payload

This approach is best if your application is structured in a way that requires the SDK to be initialized on each file in which it uses SDK methods. Before your web application can use the JSON payload of the rule artifact during SDK initialization, you should ensure the JSON payload is downloaded and is available for your application to use.

Summary of steps#

  1. Install the SDK
  2. Initialize the SDK
  3. Store and Use the JSON Payload

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 artifactDownloadSucceeded: onArtifactDownloadSucceeded,
8 artifactDownloadFailed: onArtifactDownloadFailed
9 }
10};
11
12const TargetClient = TargetClient.create(CONFIG);
13
14function onArtifactDownloadSucceeded(event) {
15 //Adobe Target SDK has now downloaded the JSON Artifact/Payload
16 console.log(event.artifactLocation) // Location from where the Artifact is downloaded.
17 console.log(event.artifactPayload) // JSON Payload which we can store locally.
18}
19
20function onArtifactDownloadFailed(event) {
21 //Adobe Target SDK has failed to download the JSON Artifact/Payload.
22 console.log(event.artifactLocation) // Location from where the Artifact is downloaded.
23 console.log(event.error.message) // Error message
24}
  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 JSON Payload#

The mechanism you use to store the JSON payload depends on your system architecture. You can use a local file, a database, or a memory object caching system such as Memcached . You need to be able to read this JSON from your application for consumption. In this guide, we use a local file as the storage.

Copied to your clipboard
1//... Code removed for brevity
2
3function onArtifactDownloadSucceeded(event) {
4 const jsonPath = 'src/config/target-rules.json'
5 fs.writeFile(jsonPath, JSON.stringify(event.artifactPayload), 'utf8', (err) => {
6 if (err) {
7 throw err;
8 };
9 console.log(`The artifact from '${event.artifactLocation}' is now saved to '${jsonPath}'`);
10 });
11}
12
13
14function onArtifactDownloadFailed(event) {
15 console.log(`The local decisioning artifact failed to download from '${event.artifactLocation}' with the following error message: ${event.error.message}`);
16}
17
18//... Code removed for brevity

Here is an example demonstrating the JSON payload-initializing capability.

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 artifactDownloadSucceeded : onArtifactDownloadSucceeded,
12 artifactDownloadFailed : onArtifactDownloadFailed
13 },
14};
15
16function onArtifactDownloadSucceeded(event) {
17 const jsonPath = 'src/config/target-rules.json'
18 fs.writeFile(jsonPath, JSON.stringify(event.artifactPayload), 'utf8', (err) => {
19 if (err) {
20 throw err;
21 };
22 console.log(`The artifact from '${event.artifactLocation}' is now saved to '${jsonPath}'`);
23 });
24}
25
26function onArtifactDownloadFailed(event) {
27 console.log(`The on-device decisioning artifact failed to download from '${event.artifactLocation}' with the following error message: ${event.error.message}`);
28}
29
30const app = express();
31const targetClient = TargetClient.create(CONFIG);
32
33app.use(cookieParser());
34
35function saveCookie(res, cookie) {
36 if (!cookie) {
37 return;
38 }
39
40 res.cookie(cookie.name, cookie.value, {maxAge: cookie.maxAge * 1000});
41}
42
43const getResponseHeaders = () => ({
44 "Content-Type": "text/html",
45 "Expires": new Date().toUTCString()
46});
47
48function sendSuccessResponse(res, response) {
49 res.set(getResponseHeaders());
50 saveCookie(res, response.targetCookie);
51 res.status(200).send(response);
52}
53
54function sendErrorResponse(res, error) {
55 res.set(getResponseHeaders());
56 res.status(500).send(error);
57}
58
59function startWebServer() {
60 app.get('/*', async (req, res) => {
61 const targetCookie = req.cookies[TargetClient.TargetCookieName];
62 const request = {
63 execute: {
64 mboxes: [{
65 address: { url: req.headers.host + req.originalUrl },
66 name: "on-device-homepage-banner" // Ensure that you have a LIVE Activity running on this location
67 }]
68 }};
69
70 try {
71 const response = await targetClient.getOffers({ request, targetCookie });
72 sendSuccessResponse(res, response);
73 } catch (error) {
74 console.error("Target:", error);
75 sendErrorResponse(res, error);
76 }
77 });
78
79 app.listen(3000, function () {
80 console.log("Listening on port 3000 and watching!");
81 });
82}
Was this helpful?
  • Privacy
  • Terms of Use
  • Do not sell my personal information
  • AdChoices
Copyright © 2022 Adobe. All rights reserved.