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.
Linking the app
Run vtex link
. For every event fired, you should see the live users retrieved from the Analytics client.
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.