To locate your write key, navigate to your front-end website source in Segment and select Settings > API Keys. Typically you'll have different front-end website sources for development, staging, and production, so make sure you're selecting the correct source. Never send data to your production source except from your production website.
Next, install Analytics.js via one of the following methods:
Copy and paste the Segment snippet into the <head> tag of your site. This snippet loads and initializes Segment asynchronously. You'll need to replace YOUR_WRITE_KEY in the snippet with the write key for your Segment front-end website source.
Install @segment/snippet from npm
Instructions for installing from npm are available in the Github repo.
Next.js: Official example
Gatsby: Community plugin
The addToCart function makes a request to the Chord API to update the customer's cart, updates the cart object with the new cart contents, and then sends a Product Added event to Segment with information about the product that was added.
Some SDK functions exist just to send tracking events to Segment for UI interactions. These functions are available via the useAnalytics hook and are always prefixed with track. Add these functions to your UI components to track customer interactions.
For example, to track when a customer views the shopping cart, you might add this code to your cart component:
If you don't add trackCartViewed to your UI component, this tracking event will not be sent to Segment. We recommend reviewing the useAnalytics SDK reference and adding all applicable functions to your website code.
For more examples of instrumenting your UI components, review Chord's website starter kits.
After you've installed Segment's Analytics.js library and added some SDK functions to your UI components, verify that Segment is receiving tracking events.
In Segment, navigate to your website front-end source, and select the Debugger tab. This shows you a live feed of tracking events that Segment is receiving.
Interact with your website, and you should see new events appear in the Debugger. Inspect the contents of an event by selecting it. Note that events have different types, including Page, Track, and Identify.
Complete this checklist to verify Segment has been installed correctly on your website.
The correct source shows your events. Events from development and staging environments should not appear in your production source, and vice versa.
The back-end source shows Track events. For example, you should see an Order Completed event when an order is completed. You should see all events in marked as sent by the OMS.
The front-end source shows Track events. For example, you should see a Product Added event when a product is added to your shopping cart, and a Cart Viewed Event when you view the cart page. You should see all events in marked as sent by the SDK, unless they are UI events that you .
The front-end source shows Page events.
Initial page load sends a Page event.
Clicking an internal link sends a Page event.
The front-end source shows Identify events when a customer enters an email address. For example, you should see an Identify event when a customer fills out a newsletter sign-up form.
I'm not seeing any events in the back-end source.
The OMS might be sending events to a different back-end source than you're expecting. Log into your OMS admin, and navigate to Settings > Stores. In the Environment Variables section, look for SEGMENT_WRITE_KEY. The write key determines the source that events are sent to.
I'm not seeing any events in the front-end source.
Check that the write key in your website code is for the correct front-end source. Next, try following Segment's troubleshooting guide for Analytics.js.
I only see a Page event when the site first loads, not when I click around.