The onload
method provides a way to register callback functions which will be executed as soon as the Web SDK has been initialized. This is useful if you would like to implement some functionality that is dependent on reading values from the Web SDK. The most common use case for this feature is to safely access the gsid
value (or any other call to get
).
gsght('onload', {callback})
Argument | Type | Example | Description |
---|---|---|---|
{callback} | Function | () => { console.log('loaded') } | A callback function to invoke as soon as the Web SDK has been initialized. |
- You can call
onload
multiple times if you would like to register multiple callbacks. - The callback will be invoked after the first call to
init
on the page completed.
Callback invocation timing
Note that the Web SDK operates in an asynchronous nature during initialization where method calls are queued locally until the full SDK payload is loaded into the page. This may mean that there is a delay between the call to
gsght('init', ...)
and the actual initialization (and thusonload
callback triggers).This initialization queue makes it so any write methods (
init
,set
,send
,onload
,update
) are safe to call at any point in time since their functionality isn't impacted by asynchronous execution. Butget
methods will fail unless they are called from within anonload
callback (or otherwise after initialization has completed)
Example
Below is an example of using an onload
callback to read values from the Web SDK.
gsght('onload', function(){
// some interaction with the Web SDK here, for example reading the gsid value
console.log(gsght('get', 'gsid'))
})