Documentation
Feedback
Guides
App Development

App Development
ServicesDeveloping services on VTEX IO
4. Using events as triggers

Now that we have successfully implemented the Analytics client, our next goal is to leverage events fired by the events-example app as triggers for requests to the Analytics client. This approach ensures that with every event received, we make a corresponding request to the Analytics client, resulting in updated data for live product visualizations every 3 seconds.

Using the Analytics client as a event handler

As the Analytics client is already implemented, we can use it in our event handler function. First, in the service-course-template/node/event/liveUsersUpdate.ts file, import the client we implemented in the previous step and the EventContext from the @vtex/api package to access the Analytics client that is within the ctx object. Finally, use the getLiveUsers() method the Analytics client implements and log the fetched data.

service-course-template/node/event/liveUsersUpdate.ts

_10
import { Clients } from '../clients/index'
_10
import { EventContext } from '@vtex/api'
_10
_10
export async function updateLiveUsers(ctx: EventContext<Clients>) {
_10
const liveUsersProducts = await ctx.clients.analytics.getLiveUsers()
_10
console.log('LIVE USERS: ', liveUsersProducts)
_10
return true
_10
}

Linking the app

Run vtex link. For every event fired, you should see the live users retrieved from the Analytics client.

service-course-template/node/event/liveUsersUpdate.ts
Terminal

_14
15:53:44.220 - info: App running service-node@6.38.1
_14
15:53:44.229 - info: Available service routes:
_14
https://myworkspace--myaccount.myvtex.com/_v/app/analytics/realTime service-node@6.38.1
_14
15:54:25.279 - info: [18:54:25.495Z] [72] myaccount/myworkspace:analytics 200 GET /_v/app/analytics/realTime 793 ms service-node@6.38.1
_14
15:54:44.772 - info: LIVE USERS: [
_14
{ slug: '1659', liveUsers: 4 },
_14
{ slug: '1837', liveUsers: 2 },
_14
{ slug: '1438', liveUsers: 1 },
_14
{ slug: '1204', liveUsers: 1 },
_14
{ slug: '1542', liveUsers: 2 },
_14
{ slug: '1352', liveUsers: 1 },
_14
{ slug: '1168', liveUsers: 3 },
_14
{ slug: '1821', liveUsers: 1 }
_14
] service-node@6.38.1

Using the Analytics client as a event handler

As the Analytics client is already implemented, we can use it in our event handler function. First, in the service-course-template/node/event/liveUsersUpdate.ts file, import the client we implemented in the previous step and the EventContext from the @vtex/api package to access the Analytics client that is within the ctx object. Finally, use the getLiveUsers() method the Analytics client implements and log the fetched data.

Linking the app

Run vtex link. For every event fired, you should see the live users retrieved from the Analytics client.

service-course-template/node/event/liveUsersUpdate.ts

_10
import { Clients } from '../clients/index'
_10
import { EventContext } from '@vtex/api'
_10
_10
export async function updateLiveUsers(ctx: EventContext<Clients>) {
_10
const liveUsersProducts = await ctx.clients.analytics.getLiveUsers()
_10
console.log('LIVE USERS: ', liveUsersProducts)
_10
return true
_10
}

Contributors
2
Photo of the contributor
Photo of the contributor
+ 2 contributors
Was this helpful?
Yes
No
Suggest edits (Github)
Contributors
2
Photo of the contributor
Photo of the contributor
+ 2 contributors
On this page