serviceapis v1.0.12
serviceapis
Loader methods for APIs of various services, currently Google Universal Analytics and YouTube.
Provides loader methods for loading the APIs of the following services safely:
Installation
Install the serviceapis
module via
npm install serviceapis --save
or
yarn add serviceapis
Usage
Google Universal Analytics
Import the loader methods with the following import statement:
import { ga as gaApi } from 'serviceapis';
// or:
var gaApi = require('serviceapis').ga;
Load the Analytics API
Load the API with the following call:
gaApi.load();
// window.ga(...) is available immediately!
Example:
gaApi.load();
ga('create', 'UA-XXXXXXXX-X', 'auto');
ga('set', 'anonymizeIp', true);
Check if Analytics API is available
Use the following call:
gaApi.isAvailable() // returns true or false
YouTube
Import the loader methods with the following import statement:
import { yt as ytApi } from 'serviceapis';
// or:
var ytApi = require('serviceapis').yt;
Load the YouTube API
Load the API with the following call:
await ytApi.load();
// the API is loaded now
...
// or:
ytApi.load()
.then(function () {
// the API is loaded now
...
});
Wait for the YouTube API
Wait for the API to become ready, but not to trigger a load
. It remains forever if load
does not get triggered.
await ytApi.availability();
// the API is loaded now
...
// or:
ytApi.availability()
.then(function () {
// the API is loaded now
...
});
Note: load
uses availability
internally. There is no need to call it again if you just called load
itself.
Check if YouTube API is available
Use the following call:
ytApi.isAvailable() // returns true or false
Create a YouTube player object
Creates a player object and waits till it is ready. Triggers a load of the API automatically in case it is necessary. Therefore, in most cases, you can use createPlayer
without load
.
const player = await ytApi.createPlayer(
'my-player',
{ videoId: 'M7lc1UVf-VE' }
);
// the player object is ready now
...
// or:
ytApi.createPlayer('my-player', { videoId: 'M7lc1UVf-VE' })
.then(function (player) {
// the player object is ready now
...
});