Personalization SDK
Configuring the Personalization SDK
Your JavaScript app’s HTML files need to incorporate the Personalization SDK (either for the Datastore local simulator or production service), as well as the main functionality of your app (for example, main.js), which also configures the Data Service, within either the <head/>
or <body/>
elements of these HTML files.
For a local simulation:
<script src="https://sdk.datastore.squiz.cloud/js/sdk-simulator.js"></script>
For your production service:
<script src="https://sdk.datastore.squiz.cloud/js/sdk.js"></script>
Configuring the Personalization SDK to use your data service in your app:
const settings = {
serviceURL: '<Your data service URL here>',
collectionName: ‘assignments',
collections: {
base: { path: ‘/' },
group: { path: ‘group/:groupid' },
user: { path: ‘user/:userid' }
},
properties: {
extends: 'extends',
extendsAll: 'extendsAll',
extendedBy: 'extendedBy',
extendedByAll: 'extendedByAll',
}
};
const datastorePersonalized = new Datastore(settings);
The settings available for the Datastore Personalization SDK are:
Setting | Required | Simulator only | Description | Type | Example |
---|---|---|---|---|---|
serviceURL |
Yes |
No |
The Data Service or Simulators URL |
String |
https://my-data-service.datastore.squiz.cloud |
jwtURL |
No |
No |
The URL to the JWT generator being used |
String |
https://my-jwt.example.com |
jwtCallback |
No |
No |
A custom callback function to get a JWT token, must return a promise |
Function |
|
jwtPayload |
No |
Yes |
Optional JWT Payload, required when using the Simulators built in JWT generator |
Object |
|
collectionName |
Yes |
No |
The collection name that the documents that will be personalized belong to |
Sting |
my-collection |
collections |
Yes |
No |
An object that defines the collections where the personalized documents live. The use of |
Object |
|
properties |
Yes |
No |
Defines what properties have been defined in the blueprint for extends properties |
Object |
|
detailedResponse |
No |
Yes |
A flag to return the detailed response of a request e.g. headers, status code etc. |
Boolean |
true |
interceptRequest |
No |
Yes |
An object to flag to return the request information (params, headers, etc) for specified paths before sending the request. |
Object |
|
failRequests |
No |
Yes |
Fail requests for specific paths with the given error object. |
Object |
Function list
Function | Description |
---|---|
Gets a DatastorePersonalizationCollectionObject reference to perform actions on the personalized collection in the Data Service. |
|
Gets a DatastorePersonalizationCollectionsObject reference to perform actions on a personalized collection in the Data Service. |
|
Gets the reference to a specific document in the base collection to perform actions on in the Data Service. |
|
Gets the reference to a specific document in the group collection to perform actions on in the Data Service. |
|
Gets the reference to a specific document in the user collection to perform actions on in the Data Service. |
|
Gets the reference to a specific base collection to perform actions on in the Data Service. |
|
Gets the reference to a specific group collection to perform actions on in the Data Service. |
|
Gets the reference to a specific user collection to perform actions on in the Data Service. |
|
Gets a DatastoreDocumentObject reference to perform actions on the document in the Data Service. |
|
Override an existing document property values in a new document in the Data Service. |
|
Gets the referenced collection/document. |
|
Returns the collection paths. |
collection()
docs()
Gets a DatastorePersonalizationCollectionsObject reference to perform actions on a personalized collection in the Data Service.
Parameters
- base
-
(boolean) Flag to include documents from the base collection.
- group
-
(boolean) Flag to include documents from the group collection.
- user
-
(boolean) Flag to include documents from the user collection.
baseDoc()
Gets the reference to a specific document in the base collection to perform actions on in the Data Service.
Parameters
- docId
-
(string) The document ID.
- pathValues
-
(Object) Optional path variables to be evaluated.
groupDoc()
Gets the reference to a specific document in the group collection to perform actions on in the Data Service.
Parameters
- docId
-
(string) The document ID.
- pathValues
-
(Object) Optional path variables to be evaluated.
Returns
DatastoreDocumentObjectReference A DatastoreDocumentObjectReference a reference to the group level personalized document to make requests on.
Example
Consider the situation where we want to get the personalized document from our group assignment collection to perform actions on, using the Personalization SDK we would do the following.
const groupAssignment = await datastorePersonalized.groupDoc(`assignment-1`, {groupID: `my-group`}).get();
userDoc()
Gets the reference to a specific document in the user collection to perform actions on in the Data Service.
Parameters
- docId
-
(string) The document ID.
- pathValues
-
(Object) Optional path variables to be evaluated.
baseCollection()
Gets the reference to a specific base collection to perform actions on in the Data Service.
Returns
DatastoreCollectionObjectReference A DatastoreCollectionObjectReference a reference to the unpersonalized collection to make requests on.
Example
Consider the situation where we want to add an unpersonalized document to our assignment collectiom, using the Personalization SDK we would do the following.
const documentData = { name: `My First Assignment` };
const baseCollection = await datastorePersonalized.baseCollection().add(documentData);
groupCollection()
Gets the reference to a specific group collection to perform actions on in the Data Service.
Returns
DatastoreCollectionObjectReference A DatastoreCollectionObjectReference a reference to the group level personalized collection to make requests on.
Example
Consider the situation where we want to add a personalized document to our course group collection, using the Personalization SDK we would do the following.
const documentData = {
name: `Group 1 - My First Assignment`,
extends: `/assignments/assignment-1`
};
const groupCollection = await datastorePersonalized.groupCollection({
groupID: `group-123`
}).add(documentData);
userCollection()
Gets the reference to a specific user collection to perform actions on in the Data Service.
Returns
DatastoreCollectionObjectReference A DatastoreCollectionObjectReference a reference to the user level personalized collection to make requests on.
Example
Consider the situation where we want to add a personalized document to our student assignment collection, using the Personalization SDK we would do the following.
const documentData = {
name: `My First Assignment`,
dueDate: `2022-12-02 00:00:00Z`,
extends: `/group/group-123/assignments/assignment-1`
};
const groupCollection = await datastorePersonalized.userCollection({
student ID: `abc-123`
}).add(documentData);
doc()
Gets a DatastoreDocumentObject reference to perform actions on the document in the Data Service.
overrideDoc()
Override an existing document property values in a new document in the Data Service.
Parameters
- docRef
-
(DatastoreDocumentObject|DatastorePersonalizedDocumentObject) The document reference to the original document.
- properties
-
(Object) The new document property values to set.
Returns
DatastoreDocumentObject A DatastoreDocumentObject to get the response from the Data Service from.
Example
Consider the situation where we want to override an assignment name for a specific student, using the Personalization SDK we would do the following.
const baseRef = datastore.Personalized.baseDoc(`assignment-1`);
const overridenDoc = await datastorePersonalized.userCollection(`abc-123`)
.overrideDoc(
baseRef,
{
assignmentName: `Student 123 - Assignment 1`
}
);
get()
Gets the referenced collection/document.
Returns
Promise<DatastoreCollectionObject|DatastoreDocumentObject> Either a DatastoreCollectionObject or DatastoreDocumentObject to get the response from the Data Service from.
Example
Consider the situation where we want to get the most relevant assignments from our assignment collections for the student with the studentID abc-123
,, using the Personalization SDK we would do the following.
const myAssignments = await datastorePersonalized.docs().get({studentID: `abc-123`});