-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyapi.jsx
161 lines (145 loc) · 4.31 KB
/
dyapi.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import {useMatches} from '@remix-run/react';
var url = require('url');
// API key and API host are taken from the envoronmental variables:
// DY_API_KEY
// DY_API_HOST
// When running locally, they come from the .env file in the project root.
// When hosted in Oxygen, they are set in the Shopify configuration.
// https://shopify.dev/docs/custom-storefronts/oxygen/storefronts/environment-variables
export async function choose(request, context, pageContext, selectors = [], isImplicitPageview = false) {
const uri = `${context.env.DY_API_HOST}/v2/serve/user/choose`;
const options = {
method: 'POST',
headers: {
'DY-API-Key': context.env.DY_API_KEY,
},
body: JSON.stringify({
selector: {
names: selectors,
},
user: {
id: getUserId(request, context),
},
session: {
dy: getSessionId(request, context),
},
context: getDyContext(request, context, pageContext),
options: {isImplicitPageview: isImplicitPageview},
}),
};
let variations = {};
try {
const response = await fetch(uri, options).then((res) => {
return res.json();
});
variations = response.choices.reduce(flattenCampaignData, {});
console.log(`Choices by campaign: ${JSON.stringify(variations, null, 2)}`);
} catch (e) {
console.error(`ERROR IN CHOOSE: ${e.message}`);
}
return variations;
}
export async function reportPageView(request, context, pageContext) {
return await choose(request, context, pageContext, [], true);
}
function getUserId(request, context) {
return '';
}
function getSessionId(request, context) {
return '';
}
function getDyContext(request, context, pageContext) {
return {
page: {...{
location: request.url,
referrer: request.referrer || '',
type: 'OTHER',
data: [],
locale: `${context.storefront.i18n.language}_${context.storefront.i18n.country}`,
}, ...pageContext},
device: {
userAgent: request.headers.get('user-agent') || '',
ip: request.headers.get('oxygen-buyer-ip'),
},
pageAttributes: url.parse(request.url, true).query,
};
}
export function useContextFromLoaders(dataKey = 'pageContext') {
const matches = useMatches();
const data= {};
matches.forEach((event) => {
const eventData = event?.data;
if (eventData && eventData[dataKey]) {
Object.assign(data, eventData[dataKey]);
}
});
return data;
}
function flattenCampaignData(res, choice) {
let data = null;
if (choice.variations.length > 0) {
switch (choice.type) {
case 'DECISION':
data = { decisionId: choice.decisionId, ...choice.variations[0].payload.data };
break;
case 'RECS_DECISION':
data = choice.variations[0].payload.data.slots.map(
slot => ({ ...slot.productData, sku: slot.sku, slotId: slot.slotId }));
break;
default:
throw new Error('Unknown choice type: ' + choice.type);
}
}
res[choice.name] = data;
return res;
}
export async function reportClick(request, context, engagement) {
try {
const options = {
method: 'POST',
url: `${context.env.DY_API_HOST}/v2/collect/user/engagement`,
headers: {
'DY-API-Key': context.env.DY_API_KEY,
},
body: {
user: {
id: getUserId(request, context),
},
session: {
dy: getSessionId(request, context),
},
engagements: [engagement],
},
json: true,
}
const response = await fetch(uri, options).then((res) => res.json());
console.log("Engagement reported: " + JSON.stringify(engagement));
} catch (e) {
console.error(`ERROR IN ENGAGEMENT: ${e.message}`);
}
}
export async function reportEvent(request, context, event) {
try {
const options = {
method: 'POST',
url: `${context.env.DY_API_HOST}/v2/collect/user/event`,
headers: {
'DY-API-Key': context.env.DY_API_KEY,
},
body: {
user: {
id: getUserId(request, context),
},
session: {
dy: getSessionId(request, context),
},
events: [event],
},
json: true
};
const response = await fetch(uri, options).then((res) => res.json());
console.log("Event reported: " + JSON.stringify(event));
} catch (e) {
console.error(`ERROR IN EVENT: ${e.message}`);
}
}