The Tracking Service provided by Headless Services is analytics tracking REST API that allows pushing Sitecore analytics events to the xDB based on client-side behavior. By default, it supports tracking Events, Goals, Outcomes, Campaigns, and Page/Route Views, and you can extend it for advanced tracking needs.
Setup
The Tracking Service is installed but disabled by default. when installing Sitecore Headless Services.
To enable the Tracker, set the Sitecore.JSS.TrackerServiceEnabled setting to true in a configuration patch file, for example:
Transform the Sitecore Web.config as follows:
In the httpCookies configuration, set requireSSL to false and sameSite to Unspecified.
In the sessionState configuration, set cookieSameSite to Unspecified.
Tracking events
You can track events using the endpoint /sitecore/api/jss/track/event?sc_apikey=[key].
The endpoint accepts POST requests with JSON payloads.
In your JSS app change LayoutServiceFactory options you can find this in src/lib/layout-service-factory.ts file.
Add or change tracking option to true
Now it’s started tracking.
Below is the code example for tracking goal, event, outcomes.
The dataFetcher ts file.
Conclusion
Enabling and using Sitecore JSS analytics tracking can greatly improve your understanding of user behavior and content performance. By setting up Sitecore JSS analytics tracking, you can gain valuable insights to enhance user experiences and achieve your business goals.
Follow the steps outlined in this guide to configure your Sitecore instance, integrate with analytics platforms, and customize tracking scripts. Continuously monitor and refine your tracking to capture the most relevant data.
With these insights, you can create more targeted content, improve engagement, and drive better outcomes for your digital initiatives.
For more information, check out these additional resources:
Sitecore JSS Documentation
Sitecore Analytics Configuration Guide
Best Practices for Using Sitecore Analytics
Sourceved Sitecore Blogs