Skip to content

Commit eca0809

Browse files
nmykhailetstetiana-karasovakweinmeisterBenjamin E. Coe
authored
chore(samples): interactive tutorials code samples for write/rejoin/purge user events (#150)
Co-authored-by: tetiana-karasova <tetiana.karasova@gmail.com> Co-authored-by: Karl Weinmeister <11586922+kweinmeister@users.noreply.github.com> Co-authored-by: Benjamin E. Coe <bencoe@google.com>
1 parent 27428ad commit eca0809

File tree

6 files changed

+389
-0
lines changed

6 files changed

+389
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main() {
18+
// [START retail_purge_user_events]
19+
20+
// Imports the Google Cloud client library.
21+
const {UserEventServiceClient} = require('@google-cloud/retail').v2;
22+
const utils = require('../setup/setup-cleanup');
23+
24+
const projectNumber = process.env['GCLOUD_PROJECT'];
25+
const visitorId = 'test_visitor_id';
26+
27+
// Placement
28+
const parent = `projects/${projectNumber}/locations/global/catalogs/default_catalog`;
29+
30+
// The filter string to specify the events to be deleted with a
31+
// length limit of 5,000 characters.
32+
const filter = `visitorId="${visitorId}"`; // TO CHECK ERROR HANDLING SET INVALID FILTER HERE
33+
34+
// Actually perform the purge.
35+
const force = true;
36+
37+
// Instantiates a client.
38+
const retailClient = new UserEventServiceClient();
39+
40+
const callPurgeUserEvents = async () => {
41+
// Construct request
42+
const request = {
43+
parent,
44+
filter,
45+
force,
46+
};
47+
48+
console.log('Purge request: ', request);
49+
50+
// Run request
51+
const [operation] = await retailClient.purgeUserEvents(request);
52+
console.log(
53+
`Purge operation in progress.. Operation name: ${operation.name}`
54+
);
55+
};
56+
57+
// Create new event
58+
const event = await utils.writeUserEvent(visitorId);
59+
console.log(
60+
`Created event ${event.eventType} with visitor id ${event.visitorId}`
61+
);
62+
63+
// Purge events
64+
await callPurgeUserEvents();
65+
// [END retail_purge_user_events]
66+
}
67+
68+
process.on('unhandledRejection', err => {
69+
console.error(err.message);
70+
process.exitCode = 1;
71+
});
72+
73+
main();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main() {
18+
// [START retail_rejoin_user_event]
19+
20+
// Imports the Google Cloud client library.
21+
const {UserEventServiceClient} = require('@google-cloud/retail').v2;
22+
const utils = require('../setup/setup-cleanup');
23+
24+
const projectNumber = process.env['GCLOUD_PROJECT'];
25+
const visitorId = 'test_visitor_id';
26+
27+
// Placement
28+
const parent = `projects/${projectNumber}/locations/global/catalogs/default_catalog`; // TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE
29+
30+
const UserEventRejoinScope = {
31+
USER_EVENT_REJOIN_SCOPE_UNSPECIFIED: 0,
32+
JOINED_EVENTS: 1,
33+
UNJOINED_EVENTS: 2,
34+
};
35+
// The type of the user event rejoin to define the scope and range of the user
36+
// events to be rejoined with the latest product catalog
37+
const userEventRejoinScope = UserEventRejoinScope.UNJOINED_EVENTS;
38+
39+
// Instantiates a client.
40+
const retailClient = new UserEventServiceClient();
41+
42+
const callRejoinUserEvents = async () => {
43+
// Construct request
44+
const request = {
45+
parent,
46+
userEventRejoinScope,
47+
};
48+
49+
console.log('Rejoin request: ', request);
50+
51+
// Run request
52+
const [operation] = await retailClient.rejoinUserEvents(request);
53+
console.log(
54+
`Rejoin operation in progress.. Operation name: ${operation.name}`
55+
);
56+
};
57+
58+
// Create new event
59+
const event = await utils.writeUserEvent(visitorId);
60+
console.log(
61+
`Created event ${event.eventType} with visitor id ${event.visitorId}`
62+
);
63+
64+
// Rejoin events
65+
await callRejoinUserEvents();
66+
67+
// Purge events
68+
utils.purgeUserEvents(parent, visitorId);
69+
// [END retail_rejoin_user_event]
70+
}
71+
72+
process.on('unhandledRejection', err => {
73+
console.error(err.message);
74+
process.exitCode = 1;
75+
});
76+
77+
main();
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
async function main() {
18+
// [START retail_write_user_event]
19+
20+
// Imports the Google Cloud client library.
21+
const {UserEventServiceClient} = require('@google-cloud/retail').v2;
22+
const utils = require('../setup/setup-cleanup');
23+
24+
const projectNumber = process.env['GCLOUD_PROJECT'];
25+
const visitorId = 'test_visitor_id';
26+
27+
// Placement
28+
const parent = `projects/${projectNumber}/locations/global/catalogs/default_catalog`; // TO CHECK ERROR HANDLING PASTE THE INVALID CATALOG NAME HERE
29+
30+
// User event to write
31+
const userEvent = {
32+
eventType: 'home-page-view',
33+
visitorId,
34+
eventTime: {
35+
seconds: Math.round(Date.now() / 1000),
36+
},
37+
};
38+
39+
// Instantiates a client.
40+
const retailClient = new UserEventServiceClient();
41+
42+
const callWriteUserEvent = async () => {
43+
// Construct request
44+
const request = {
45+
userEvent,
46+
parent,
47+
};
48+
49+
console.log('Write request: ', request);
50+
51+
// Run request
52+
const response = await retailClient.writeUserEvent(request);
53+
console.log(`Operation result: ${JSON.stringify(response[0])}`);
54+
};
55+
56+
// Create new user event
57+
console.log('Start to write user event');
58+
await callWriteUserEvent();
59+
console.log('Write operation finished');
60+
61+
// Purge user events by visitor id
62+
await utils.purgeUserEvents(parent, visitorId);
63+
// [END retail_write_user_event]
64+
}
65+
66+
process.on('unhandledRejection', err => {
67+
console.error(err.message);
68+
process.exitCode = 1;
69+
});
70+
71+
main();
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const path = require('path');
18+
const cp = require('child_process');
19+
const {before, describe, it} = require('mocha');
20+
const {assert} = require('chai');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
23+
24+
const cwd = path.join(__dirname, '..');
25+
26+
describe('Purge user events', () => {
27+
const eventType = 'detail-page-view';
28+
const visitorId = 'test_visitor_id';
29+
let stdout;
30+
31+
before(async () => {
32+
stdout = execSync(
33+
'node interactive-tutorials/events/purge-user-events.js',
34+
{cwd}
35+
);
36+
});
37+
38+
it('should check that new event already created', () => {
39+
const regex = new RegExp(
40+
`Created event ${eventType} with visitor id ${visitorId}`,
41+
'g'
42+
);
43+
assert.match(stdout, regex);
44+
});
45+
46+
it('should check that purge operation started', () => {
47+
const regex = new RegExp('Purge operation in progress', 'g');
48+
assert.match(stdout, regex);
49+
});
50+
});
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const path = require('path');
18+
const cp = require('child_process');
19+
const {before, describe, it} = require('mocha');
20+
const {assert} = require('chai');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
23+
24+
const cwd = path.join(__dirname, '..');
25+
26+
describe('Rejoin user events', () => {
27+
const eventType = 'detail-page-view';
28+
const visitorId = 'test_visitor_id';
29+
let stdout;
30+
31+
before(async () => {
32+
stdout = execSync(
33+
'node interactive-tutorials/events/rejoin-user-events.js',
34+
{cwd}
35+
);
36+
});
37+
38+
it('should check that new event already created', () => {
39+
const regex = new RegExp(
40+
`Created event ${eventType} with visitor id ${visitorId}`,
41+
'g'
42+
);
43+
assert.match(stdout, regex);
44+
});
45+
46+
it('should check that rejoin operation started', () => {
47+
const regex = new RegExp('Rejoin operation in progress', 'g');
48+
assert.match(stdout, regex);
49+
});
50+
51+
it('should check that purge operation started', () => {
52+
const regex = new RegExp('Purge operation in progress', 'g');
53+
assert.match(stdout, regex);
54+
});
55+
});
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2022 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
const path = require('path');
18+
const cp = require('child_process');
19+
const {before, describe, it} = require('mocha');
20+
const {assert, expect} = require('chai');
21+
22+
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});
23+
24+
const cwd = path.join(__dirname, '..');
25+
26+
describe('Write user event', () => {
27+
const eventType = 'home-page-view';
28+
const visitorId = 'test_visitor_id';
29+
let stdout;
30+
31+
before(async () => {
32+
stdout = execSync('node interactive-tutorials/events/write-user-event.js', {
33+
cwd,
34+
});
35+
});
36+
37+
it('should check that write operation started', () => {
38+
const regex = new RegExp('Start to write user event', 'g');
39+
assert.match(stdout, regex);
40+
});
41+
42+
it('should check that new event created correctly', () => {
43+
const regex = new RegExp('Operation result: .*\\n', 'g');
44+
assert.match(stdout, regex);
45+
const string = stdout
46+
.match(regex)
47+
.toString()
48+
.replace('Operation result: ', '');
49+
const event = JSON.parse(string);
50+
expect(event.eventType).to.be.equal(eventType);
51+
expect(event.visitorId).to.be.equal(visitorId);
52+
});
53+
54+
it('should check that write operation finished', () => {
55+
const regex = new RegExp('Write operation finished', 'g');
56+
assert.match(stdout, regex);
57+
});
58+
59+
it('should check that purge operation started', () => {
60+
const regex = new RegExp('Purge operation in progress', 'g');
61+
assert.match(stdout, regex);
62+
});
63+
});

0 commit comments

Comments
 (0)