Chord OMS
...
Next.js
SDK Reference
useAnalytics
18min
the useanalytics hook returns a set of functions that send a tracking event to segment the starter sends most segment events about user actions automatically, when other hooks like usecart are used however, some events need to be closely tied to the ui, like click tracking, and must be sent explicitly by react components functions for sending these events are available via the useanalytics hook example import { useanalytics } from ' /hooks/actions' const { trackcartviewed, trackcollectionclicked, trackemailcaptured, trackproductclicked, trackproductlistfiltered, trackproductlistviewed, trackproductviewed, trackpromotionclicked, trackpromotionviewed, trackstockrequestcreated } = useanalytics() returns the useanalytics hook returns an object with these properties property description trackcartviewed a function that sends a cart viewed event to segment trackcollectionclicked a function that sends a collection clicked event to segment trackemailcaptured a function that sends an email captured event to segment trackproductclicked a function that sends a product clicked event to segment trackproductlistfiltered a function that sends a product list filtered event to segment trackproductlistviewed a function that sends a productlistviewed event to segment trackproductviewed a function that sends a product viewed event to segment trackpromotionclicked a function that sends a promotion clicked event to segment trackpromotionviewed a function that sends a promotion viewed event to segment trackstockrequestcreated a function that sends a stock request created event to segment functions trackcartviewed the trackcartviewed function sends a cart viewed event to segment arguments none returns undefined example import { useanalytics } from ' /hooks/actions' const { trackcartviewed } = useanalytics() trackcartviewed() trackcollectionclicked the trackcollectionclicked function sends a collection clicked event to segment arguments trackcollectionclicked({ id, description, imageurl, slug, title }) argument type description options id string id of the collection options description? string (optional) description of the collection options imageurl? string (optional) url of an image for the collection options slug? string (optional) slug of the collection options title? string (optional) title of the collection returns undefined example import { useanalytics } from ' /hooks/actions' const { trackcollectionclicked } = useanalytics() trackcollectionclicked({ id 'red collection', title 'red' }) trackemailcaptured the trackemailcaptured function sends an email captured event to segment arguments trackemailcaptured({ email, placement }) argument type description options email string email that was captured options placement? component? string (optional) page component/module that captured the email options placement? page? string (optional) page that captured the email returns undefined example import { useanalytics } from ' /hooks/actions' const { trackemailcaptured } = useanalytics() trackemailcaptured({ email 'test\@test com', placement { component 'footer', page '/shop' } }) trackproductclicked the trackproductclicked function sends a product clicked event to segment arguments trackproductclicked({ producthandle }) argument type description options producthandle string shopify product handle of the product returns undefined example import { useanalytics } from ' /hooks/actions' const { productclicked } = useanalytics() trackproductclicked({ producthandle 'product 1' }) trackproductlistfiltered the trackproductlistfiltered function sends a product list filtered event to segment arguments trackproductlistfiltered({ filter, list, sort }) argument type description options filter type string selected filter type options filter value string selected filter value options list category string product list category options list id string product list id options sort type string product list sort type options sort value string product list sort value returns undefined example import { useanalytics } from ' /hooks/actions' const { trackproductlistfiltered } = useanalytics() trackproductlistfiltered({ filter { type 'color', value 'red' }, list { category 'shirt', id '123' }, sort { type 'ascending', value 'price' } }) trackproductlistviewed the trackproductlistviewed function sends a product list viewed event to segment arguments trackproductlistviewed({ list }) argument type description options list category string product list category options list id string product list id returns undefined example import { useanalytics } from ' /hooks/actions' const { trackproductlistviewed } = useanalytics() trackproductlistviewed({ list { category 'shirt', id '123' } }) trackproductviewed the trackproductviewed function sends a product viewed event to segment arguments trackproductviewed({ producthandle }) argument type description options producthandle string shopify product handle of the product returns undefined example import { useanalytics } from ' /hooks/actions' const { trackproductviewed } = useanalytics() trackproductviewed({ producthandle 'product 1' }) trackpromotionclicked the trackpromotionclicked function sends a promotion clicked event to segment arguments trackpromotionclicked({ creative, id, name, position }) argument type description options? creative? string (optional) creative attached to promotion options? id? string (optional) id of the promotion options? name? string (optional) name of the promotion options? position? string (optional) position of the promotion on the site returns undefined example import { useanalytics } from ' /hooks/actions' const { trackpromotionclicked } = useanalytics() trackpromotionclicked({ creative 'apple', id '123', name 'red shirt sale', position 'banner' }) trackpromotionviewed the trackpromotionviewed function sends a promotion viewed event to segment arguments trackpromotionviewed({ creative, id, name, position }) argument type description options? creative? string (optional) creative attached to promotion options? id? string (optional) id of the promotion options? name? string (optional) name of the promotion options? position? string (optional) position of the promotion on the site returns undefined example import { useanalytics } from ' /hooks/actions' const { trackpromotionviewed } = useanalytics() trackpromotionviewed({ creative 'apple', id '123', name 'red shirt sale', position 'banner' }) trackstockrequestcreated the trackstockrequestcreated function sends a stock request created event to segment arguments trackstockrequestcreated({ email, producthandle }) argument type description options email string the email address to sent an email to when the product is back in stock options producthandle string the shopify product handle returns undefined example import { useanalytics } from ' /hooks/actions' const { trackstockrequestcreated } = useanalytics() trackstockrequestcreated({ email 'test\@test com', producthandle 'product 1' })