Fetching the user profile requires an with search, browse and recommendation ACLs.Don’t expose this API key in public clients (for example, in a web or mobile app). Instead, fetch the user profile from a secure backend server.
1
Retrieve your user's ID
Retrieve your user’s ID from where it’s stored (for example, your database).If a user ID is unavailable (possibly because the user is a first-time visitor) or doesn’t correspond to an existing user profile, you can implement a fallback to avoid adding extra logic to your app.The following example fetches the user profile and
provides a fallback if there’s a problem completing the :
Ensure the user profile is available to the rest of your app.In React, you can provide the user profile to your app as follows:
React
Report incorrect code
Copy
import { createContext, useContext } from "react";const PersonalizationProfileContext = createContext(null);PersonalizationProfileContext.displayName = "PersonalizationProfileContext";function usePersonalizationProfile() { const context = useContext(PersonalizationProfileContext); if (!context) { throw new Error( "usePersonalizationProfile() must be used within a <PersonalizationProfileProvider>." ); } return context;}function PersonalizationProfileProvider({ userProfile, children }) { return ( <PersonalizationProfileContext.Provider value={userProfile}> {children} </PersonalizationProfileContext.Provider> );}function App({ userProfile }) { return ( <PersonalizationProfileProvider userProfile={userProfile}> <Page /> </PersonalizationProfileProvider> );}
3
Access the user profile
After passing the user profile to the PersonalizationProfileProvider provider,
you can access your user profile properties with the usePersonalizationProfile React Hook.
You can use this Hook in any component within PersonalizationProfileProvider.
React
Report incorrect code
Copy
function Page() { const { userID, affinities } = usePersonalizationProfile(); return <>{/* Your React */}</>;}