react-sprie-sdk
v1.1.2033
Published
`Version : 1.1.2031`
Downloads
18
Readme
Sprie SDK
Version : 1.1.2031
Quick Start
Make sure you acquire apikey from Sprie and have a product registered and catalogued by Sprie beforehand.
<!-- STEP 1. Load React CDN Scripts -->
<script crossorigin="" src="https://unpkg.com/react@17/umd/react.production.min.js"></script>
<script crossorigin="" src="https://unpkg.com/react-dom@17/umd/react-dom.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prop-types/15.8.0/prop-types.min.js" integrity="sha512-M1OTu9xD1JPdXo2cOeJI+Z/f8P6E/pqK9ug3G8PRNLw1caUePewEmpFYKSYh4LEz483qnyB/UTlgX1Q4VCEsKg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<!-- STEP 2. Load Sprie SDK -->
<script src="https://cdn.jsdelivr.net/npm/react-sprie-sdk"> </script>
<!-- STEP 3. Intialize -->
<script>
SprieSDK.Loader.Init({ apiKey:"xxx-xxx-xxx" });
</script>
<!-- STEP 6. Trigger -->
<button onclick="SprieSDK.Loader.Load('product-sku');"> Preview </button>
SDK Reference
Sprie SDK is built on React and is released as umd library which depends on react libs (must be loaded from external cdn).
To support both react projects and vanilla JS projects, SprieSDK lib has a Loader
object which has following methods to run the react SDK as native vanilla SDK.
Methods
- Init ({apiKey})
- Load (sku)
- Check (sku)
- CheckBatch (skuArray)
- UnmountAll ( )
- GetSprieLink ( sku )
- HideWidget ( )
Init ( {apiKey } )
SprieSDK.Loader.Init( {apiKey:'xxx-xxx-xxx'} );
Must be called only once for apikey authentication.
N.B. Must not be called every time product is loaded.
Params
- apiKey : string - apiKey received from Sprie Cloud
Load ( sku )
SprieSDK.Loader.Load( 'your-product-sku' );
Call everytime you want to load a new asset on AR on Sprie. Only SKU should be provided.
CheckSKU ( sku )
SprieSDK.Check( 'your-product-sku' );
(optional). Can be called to check if the product is registered with Sprie or not. For bulk assets, refer to CheckBatch. Should be used after authentication is done.
Response : {<sku>:<boolean>}
Response Example : {"centena-bert-stone":true}
CheckSKUBatch ( skuArray )
SprieSDK.CheckSKUBatch( ['your-product-sku', 'your-product-sku2'] );
(optional). Can be called to check if an array of product skus is registered with Sprie or not. Should be used after authentication is done.
Response : [{<sku>:<boolean>]}
Response Example :
[
{ "your-product-sku":true }, // this sku exists
{ "your-product-sku2": false} // this doesn't
]
UnmountAll ( )
SprieSDK.Loader.UnmountAll( );
Removes current instance of SprieSDK from website. This action is stateless, which means, refreshing the page will re-mount it again.
GetSprieLink (sku)
SprieSDK.GetSprieLink (sku)
Fetches the share link of the product (with the product page), typically to share it via an app, or show a QR code. this link appends sprie-sku
query param with the given sku which helps sprie-widget to open automatically.
HideWidget ( )
SprieSDK.HideWidget ( );
Hides the widget as well as the widget icon.
Events
All Events are based on CustomEvent
or Event
. You can find the respective data for particular event by standard event.detail.[field]
.
Auth
- SprieEvent:onAuthLoading ({isLoading: boolean}) - ✅
- SprieEvent:onAuthOk () - ✅
- SprieEvent:onAuthErr ({err: string}) - ✅
- SprieEvent:onSDKReady () - ✅
- SprieEvent:onSDKErr ({err: string}) - ✅
IO
- SprieEvent:onCameraAllowed ( ) - ✅
- SprieEvent:onCameraDenied ( ) - ✅
- SprieEvent:onCameraError ({err: string}) - ✅
Assets
- SprieEvent:onAssetCart ({sku: string}) - ✅
- SprieEvent:onAssetWishlist ({sku : string}) - ✅
- SprieEvent:onAssetLoading ({sku: string, isLoading: boolean}) - ✅
- SprieEvent:onAssetLoadOk ({sku: string}) - ✅
- SprieEvent:onAssetLoadErr ({sku: string, err: string}) - ✅
- SprieEvent:onCheckAssetSKU ({[sku]:boolean}) - ✅ . e.g. Response : {'your-product-sku':true} // if it already exists
- SprieEvent:onCheckAssetSKUBatch ({[sku]:boolean, ...}) - ✅ . e.g. Response : {'your-product-sku':true, 'sku-2':false}
Widget
- SprieEvent:onWidgetMinimised ( ) - ✅
Events Example
<script>
SprieSDK.Loader.Init({ apiKey:"xxx-xxx-xxx" });
document.addEventListener("SprieEvent:onAssetCart", function (e) {
alert("Added to cart " + e.detail.sku);
});
document.addEventListener("SprieEvent:onCameraDenied", function (e) {
alert("Camera Denied!");
});
document.addEventListener("SprieEvent:onCameraError", function (e) {
alert("Camera Error : " + e.detail.err);
});
</script>
Advanced Examples
Check particular SKU before rendering the preview trigger button
This code checks the sku with Sprie Network and hides/shows a preview button based on the result. The checking code can return a promise and triggers a convinient event as well (where promises are not supported). This check method must be called only after authentication is successful otherwise unauthenticated API calls will return inconsistent result.
<script>
const sku = 'my-precious-sku';
let showPreviewButton = false;
const onAuthDone = function (){
SprieSDK.CheckSKU(sku);
}
const onCheckSKU = function (e){
const result = e.detail; // {'my-precious-sku':true}
if(result && Object.keys(result)[0]===sku){
showPreviewButton = result[sku];
}
}
document.addEventListener("SprieEvent:onAuthOk", onAuthDone);
document.addEventListener("SprieEvent:onCheckAssetSKU", onCheckSKU);
SprieSDK.Loader.Init({ apiKey:"xxx-xxx-xxx" });
</script>
Check array of SKUs on product list page
Similar to CheckSKU
method. but takes an array of skus. This method is in alpha. Expect future changes.
<script>
const arrSku = ['my-precious-sku','my-secondary-sku'];
let validSkus = [];
const onAuthDone = function (){
SprieSDK.CheckSKUBatch(arrSku);
}
const onCheckSKUBatch = function (e){
const result = e.detail; // [{ 'my-precious-sku':true, 'my-secondary-sku':false }]
validSkus = Object.keys(result).map(x=>x).filter(x=>result[x]).map(x=>x); // validSkus=['my-precious-sku'];
}
document.addEventListener("SprieEvent:onAuthOk", onAuthDone);
document.addEventListener("SprieEvent:onCheckAssetSKUBatch", onCheckSKUBatch);
SprieSDK.Loader.Init({ apiKey:"xxx-xxx-xxx" });
</script>
Hide widget on minimise
This code hides the widget on receiving the onWidgetMinimised
event callback
<script>
document.addEventListener("SprieEvent:onWidgetMinimised", function () {
SprieSDK.Loader.HideWidget();
});
</script>
Reset CDN cache
https://purge.jsdelivr.net/npm/react-sprie-sdk@latest
Test update version
https://cdn.jsdelivr.net/npm/react-sprie-sdk@latest