通过Adobe Target SDK自动下载、存储和更新规则构件

当您能够同时初始化Adobe Target SDK并启动Web服务器时,此方法将最有效。 在Web服务器应用程序开始为请求提供服务之前,规则工件将由Adobe Target SDK下载并缓存到内存中。 一旦您的Web应用程序启动并运行,将使用内存中规则工件执行所有Adobe Target决策。 将基于您在SDK初始化步骤中指定的pollingInterval更新缓存的规则构件。

步骤摘要

  1. 安装SDK
  2. 初始化SDK
  3. 存储和使用规则构件

1.安装SDK

NPM
code language-javascript line-numbers
npm i @adobe/target-nodejs-sdk -P
MVN
code language-javascript line-numbers
<dependency>
    <groupId>com.adobe.target</groupId>
    <artifactId>java-sdk</artifactId>
    <version>1.0</version>
</dependency>

2.初始化SDK

  1. 首先,导入SDK。 导入到用于控制服务器启动的同一文件。

    Node.js

    code language-javascript line-numbers
       const TargetClient = require("@adobe/target-nodejs-sdk");
    

    Java

    code language-javascript line-numbers
       import com.adobe.target.edge.client.ClientConfig;
    import com.adobe.target.edge.client.TargetClient;
    
  2. 要配置SDK,请使用创建方法。

    Node.js

    code language-javascript line-numbers
       const CONFIG = {
        client: "<your target client code>",
        organizationId: "your EC org id",
        decisioningMethod: "on-device",
        pollingInterval : 300000,
        events: {
            clientReady: startWebServer
        }
    };
    
    const TargetClient = TargetClient.create(CONFIG);
    
    function startWebServer() {
        //Adobe Target SDK has now downloaded the JSON Artifacts and is available in the memory.
        //You can start your web server now to serve requests now.
    }
    

    Java

    code language-javascript line-numbers
       ClientConfig config = ClientConfig.builder()
        .client("<you target client code>")
        .organizationId("<your EC org id>")
        .build();
    TargetClient targetClient = TargetClient.create(config);
    
  3. 通过导航到​ Administration > Implementation,可从Adobe Target中检索客户端和organizationId,如下所示。

    <! — 插入image-client-code.png —>
    Target中“管理”下的 实现页面

3.存储和使用规则构件

您无需自己管理规则构件,调用SDK方法应简单明了。

Node.js
code language-javascript line-numbers
//req is the request object from the server request listener method
const targetCookie = req.cookies[TargetClient.TargetCookieName];
const request = {
    context: {
        channel: "web"
    },
    execute: {
        mboxes: [{
            address: { url: req.headers.host + req.originalUrl },
            name: "on-device-homepage-banner"
        }],
    },
};

TargetClient.getOffers({
    request,
    targetCookie
}).then(function(response) {
    //This Target response is coming from the In-memory Target artifact.
    console.log("Target response", response);
}).catch(function(err) {
    console.error("Target:", err);
})
Java
code language-java line-numbers
MboxRequest mbox = new MboxRequest().name("homepage").index(0);
TargetDeliveryRequest request = TargetDeliveryRequest.builder()
    .context(new Context().channel(ChannelType.WEB))
    .execute(new ExecuteRequest().mboxes(Arrays.asList(mbox)))
    .build();
TargetDeliveryResponse response = targetClient.getOffers(request);
NOTE
在上述代码示例中,TargetClient对象保留对内存中规则工件的引用。 当您使用此对象来调用标准SDK方法时,它会使用内存中规则工件进行决策。 如果您的应用程序的结构决定了您需要在其他文件中调用SDK方法,而这些文件需要初始化和侦听客户端请求,并且如果这些文件无权访问TargetClient对象,则您可以下载JSON有效负载并将其存储在本地JSON文件中,以供其他文件使用,这些文件需要初始化SDK。 有关使用JSON有效负载🔗下载规则工件的部分,将对此进行说明。

以下是初始化Adobe Target SDK后启动Web应用程序的示例。

Node.js
code language-javascript line-numbers
const express = require("express");
const cookieParser = require("cookie-parser");
const TargetClient = require("@adobe/target-nodejs-sdk");
const CONFIG = {
    client: "<your target client code>",
    organizationId: "your EC org id",
    decisioningMethod: "on-device",
    pollingInterval : 300000,
    events: {
        clientReady: startWebServer
    }
};

const app = express();
const targetClient = TargetClient.create(CONFIG);

app.use(cookieParser());

function saveCookie(res, cookie) {
  if (!cookie) {
    return;
  }

  res.cookie(cookie.name, cookie.value, {maxAge: cookie.maxAge * 1000});
}

const getResponseHeaders = () => ({
  "Content-Type": "text/html",
  "Expires": new Date().toUTCString()
});

function sendSuccessResponse(res, response) {
  res.set(getResponseHeaders());
  saveCookie(res, response.targetCookie);
  res.status(200).send(response);
}

function sendErrorResponse(res, error) {
  res.set(getResponseHeaders());
  res.status(500).send(error);
}

function startWebServer() {
    app.get('/*', async (req, res) => {
    const targetCookie = req.cookies[TargetClient.TargetCookieName];
    const request = {
        execute: {
        mboxes: [{
            address: { url: req.headers.host + req.originalUrl },
            name: "on-device-homepage-banner" // Ensure that you have a LIVE Activity running on this location
        }]
        }};

    try {
        const response = await targetClient.getOffers({ request, targetCookie });
        sendSuccessResponse(res, response);
    } catch (error) {
        console.error("Target:", error);
        sendErrorResponse(res, error);
    }
    });

    app.listen(3000, function () {
    console.log("Listening on port 3000 and watching!");
    });
}
Java
code language-java line-numbers
import com.adobe.target.edge.client.ClientConfig;
import com.adobe.target.edge.client.TargetClient;
import com.adobe.target.delivery.v1.model.ChannelType;
import com.adobe.target.delivery.v1.model.Context;
import com.adobe.target.delivery.v1.model.ExecuteRequest;
import com.adobe.target.delivery.v1.model.MboxRequest;
import com.adobe.target.edge.client.entities.TargetDeliveryRequest;
import com.adobe.target.edge.client.model.TargetDeliveryResponse;

@Controller
public class TargetController {

  private TargetClient targetClient;

  TargetController() {
    // You should instantiate TargetClient in a Bean and inject the instance into this class
    // but we show the code here for demonstration purpose.
    ClientConfig config = ClientConfig.builder()
        .client("<you target client code>")
        .organizationId("<your EC org id>")
        .build();
    targetClient = TargetClient.create(config);
  }

  @GetMapping("/")
  public String homePage() {
    MboxRequest mbox = new MboxRequest().name("homepage").index(0);
    TargetDeliveryRequest request = TargetDeliveryRequest.builder()
        .context(new Context().channel(ChannelType.WEB))
        .execute(new ExecuteRequest().mboxes(Arrays.asList(mbox)))
        .build();
    TargetDeliveryResponse response = targetClient.getOffers(request);
    // ...
  }
}
recommendation-more-help
6906415f-169c-422b-89d3-7118e147c4e3