Skip to content

Commit ee7d4f0

Browse files
jasonwaterscromulentlabs5amfungSam Fung
authored
new samples for 2.0 (#9)
* local execution sample * target local execution sample update * updated local-execution sample * get attributes sample * added feature-flag link * formatting * updated samples and docs * updated local-exeecution and feature-flag samples * updated local-exeecution and feature-flag samples * new swapi for feature-flag sample * alpha.4 * alpha.5 * Fix typo * updated local decisioning samples to support 2.0.0-alpha.9 * 2.0.0-alpha.12 * update to use events option (#2) * 2.0.0-alpha.14 * Rename to on-device decisioning (#3) Co-authored-by: Sam Fung <[email protected]> Co-authored-by: Greg <[email protected]> Co-authored-by: Sam Fung <[email protected]> Co-authored-by: Sam Fung <[email protected]>
1 parent a7d5076 commit ee7d4f0

21 files changed

+2022
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
node_modules
33
jspm_packages
44
.idea
5+
.DS_Store

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ when the test is started on the server side and then it is handed off to at.js o
2828
Node.js SDK API calls when processing a client request, sharing the same ECID instance.
2929
- [multiple-mbox-ecid-analytics-atjs-integration](multiple-mbox-ecid-analytics-atjs-integration) - shows how Target
3030
Node.js SDK can be used to request content for multiple mboxes in the same Target call.
31+
- [on-device-decisioning](on-device-decisioning) - shows how Target Node.js SDK can be used in on-device decisioning method
32+
- [feature-flag](feature-flag) - shows how Target Node.js SDK can be easily used for feature flags
3133

3234
For Target Node.js SDK documentation, see [Target Node.js SDK NPM page](https://www.npmjs.com/package/@adobe/target-nodejs-sdk).
3335

feature-flag/README.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# Target feature flag sample
2+
3+
## Overview
4+
5+
For this sample, we first created two simple AB activities. One to represent feature flags for engineering purposes and another for marketing purposes. Each activity has two experiences that have JSON offer content. The JSON holds unique key value pairs that are used by the sample app to determine what engineering systems to use and marketing content to show.
6+
7+
### Engineering Feature Flags Activity
8+
mbox: `demo-engineering-flags`
9+
10+
#### Experience A
11+
```json
12+
{
13+
"cdnHostname": "cdn.cloud.corp.net",
14+
"searchProviderId": "starwars",
15+
"hasLegacyAccess": false
16+
}
17+
```
18+
19+
#### Experience B
20+
```json
21+
{
22+
"cdnHostname": "cdn.megacloud.corp.com",
23+
"searchProviderId":"startrek",
24+
"hasLegacyAccess": true
25+
}
26+
```
27+
28+
### Marketing Activity
29+
mbox: `demo-marketing-offer1`
30+
31+
### Experience A
32+
```json
33+
{
34+
"experience": "A",
35+
"asset": "demo-marketing-offer1-exp-A.png"
36+
}
37+
```
38+
### Experience B
39+
40+
```json
41+
{
42+
"experience": "B",
43+
"asset": "demo-marketing-offer1-exp-B.png"
44+
}
45+
```
46+
47+
When run, the sample app displays a marketing banner and a search box. The marketing banner is different depending on the `asset` value of the `demo-marketing-offer1` mbox. And the search experience differs depending on the `searchProviderId` value of the `demo-engineering-flags` mbox. If the value is `starwars`,a [Star Wars API](https://swapi.co/) is used to search for characters. If the value is `startrek`, a [Star Trek API](http://stapi.co/) is used to search for characters.
48+
49+
In this sample, the `getAttributes` call is used to greatly simplify accessing the JSON offer. Typically, a developer would need to find the JSON offer object in the response of the `getOffers` call. This is done in other samples. It is straightforward, but can be cumbersome -- and it requires developers to be intimately familiar with the SDK response object.
50+
51+
Instead, this sample uses the `getAttributes` call to get the offer instead. It then looks up the value of each attribute using one of the helper methods.
52+
53+
## Running the sample
54+
1. Install dependencies: `npm i`
55+
2. Start: `npm start`
56+
3. Point a browser to http://127.0.0.1:3000
57+
58+
59+
## How it works
60+
61+
In the code sample below, take a look at the `getAttributes` call. An array of mbox names and an options object is passed in. The result is an attributes object with a few methods that can be used to get offer details.
62+
63+
The `getValue` method is used to get the `searchProviderId` from the `demo-engineering-flags` mbox offer.
64+
65+
And the `asObject` method is used to get a plain old JSON representation of the `demo-marketing-offer1` mbox offer.
66+
67+
```js
68+
const targetClient = TargetClient.create(CONFIG);
69+
const offerAttributes = await targetClient.getAttributes([
70+
"demo-engineering-flags",
71+
"demo-marketing-offer1",
72+
], { targetCookie });
73+
74+
75+
//returns just the value of searchProviderId from the mbox offer
76+
const searchProviderId = offerAttributes.getValue("demo-engineering-flags", "searchProviderId");
77+
78+
//returns a simple JSON object representing the mbox offer
79+
const marketingOffer = offerAttributes.asObject("demo-marketing-offer1");
80+
81+
// the value of marketingOffer looks like this
82+
// {
83+
// "experience": "A",
84+
// "asset": "demo-marketing-offer1-exp-A.png"
85+
// }
86+
87+
```
88+
89+
Note: This sample uses on-device decisioning method. But the `getAttributes` method can be used in any decisioning method.

feature-flag/index.handlebars

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>{{pageTitle}}</title>
6+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
7+
<link rel="icon" href="https://wwwimages2.adobe.com/favicon.ico" type="image/x-icon">
8+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
9+
<link rel="stylesheet" href="style.css" type="text/css">
10+
<meta name="viewport" content="width=device-width, initial-scale=1">
11+
</head>
12+
<body>
13+
<div class="container">
14+
{{#if offer}}
15+
<div class="py-5">
16+
<h1>{{pageTitle}}</h1>
17+
<p class="lead">Experience {{offer.experience}}</p>
18+
<img src="{{offer.asset}}" class="rounded offer" />
19+
</div>
20+
{{/if}}
21+
22+
<div class="py-3">
23+
<h2>Search</h2>
24+
<form class="form-inline">
25+
<div class="form-group mx-sm-3 mb-2">
26+
<input class="form-control searchbox" type="text" placeholder="Search for {{search.domain}}&hellip;" name="search" value="{{search.result.term}}">
27+
</div>
28+
<button type="submit" class="btn btn-primary mb-2">Search</button>
29+
</form>
30+
{{#if search.result}}
31+
<p><small>{{search.result.message}}</small></p>
32+
<table class="table table-sm table-striped">
33+
<thead>
34+
<tr>
35+
<th scope="col">Name</th>
36+
<th scope="col">Gender</th>
37+
<th scope="col">Birth Year</th>
38+
</tr>
39+
</thead>
40+
<tbody>
41+
{{#if search.result.list.length }}
42+
{{#each search.result.list}}
43+
<tr>
44+
<td>{{this.name}}</td>
45+
<td>{{this.gender}}</td>
46+
<td>{{this.birth_year}}</td>
47+
</tr>
48+
{{/each}}
49+
{{else}}
50+
<tr><td colspan="999">No results.</td></tr>
51+
{{/if}}
52+
</tbody>
53+
</table>
54+
{{/if}}
55+
</div>
56+
57+
<div class="py-3">
58+
<h2>Flags</h2>
59+
<pre id="result" {{#if error}}class="error"{{/if}}>{{flags}}</pre>
60+
</div>
61+
62+
<div class="py-3" style="display: none">
63+
<h2>Target Response</h2>
64+
<pre id="result" {{#if error}}class="error"{{/if}}>{{targetResponse}}</pre>
65+
</div>
66+
</div>
67+
</body>
68+
</html>

0 commit comments

Comments
 (0)