透過Adobe Target SDK自動下載、儲存和更新規則成品

此方法最適合您在初始化Adobe Target SDK的同時初始化Web伺服器並啟動它。 規則成品將由Adobe Target SDK下載並快取到記憶體中,然後您的Web伺服器應用程式才會開始提供要求。 一旦您的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,請使用create方法。

    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. 使用者端和organizationId均可透過導覽至「Administration > Implementation」從Adobe Target擷取,如下所示。

    <! — 插入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後啟動網頁應用程式的範例。

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