Note: This guide is written for a developer audience. If you are looking for the simpler Metomic Auto mode, click here
The Metomic library can be loaded into your application either:
<link rel="stylesheet" href="https://metomic.io/metomic.css"/><script src="https://metomic.io/metomic.js"></script>
// Metomic is now available as a global objectvar metomic = new Metomic('YOUR_CLIENT_ID')
npm install --save metomic-js
If you use
const Metomic = require('metomic-js');const metomic = new Metomic('YOUR_CLIENT_ID')
You will need to ensure that the css is also loaded into your page. It is bundled with metomic-js under
Or if you use ES6 modules:
import Metomic from 'metomic-js'// And, assuming your bundler can import css:import 'metomic-js/dist/main.css'const metomic = new Metomic('YOUR_CLIENT_ID')
To use the above snippets, you must replace
YOUR_CLIENT_ID with your own
clientId. This can be found the in the Developer section of the dashboard.
The Metomic SDK exposes a few methods to help manage the consent-based behaviour of your app. All of the following examples assume you have intialised an instance of the
Metomic class into a value named
Metomic will work for both your authenticated and non-authenticated users, but with a caveat:
To manage, audit and surface consents, Metomic needs an
id for the user. In the anonymous case, when no
id has been provided, Metomic will create it's own - but it is more likely that you will want to query Metomic for the consents of a particular user.
In order to do this securely, it is important that Metomic can trust that the identity the client claims to have is indeed legitimate. For this purpose there is an API method that builds a token for a particular userId, which you can pass in to
It is recommended that you generate this token from your backend whenever a user signs in, and pass it forward to the client.
This binds the Metomic session to a particular user. See Authenticating a user section above.
metomic.requestConsentFor(policyId,[cb]) : Promise
Explicitly triggers a modal dialog requesting the user's consent for the policy with id
policyId. Returns a promise which is resolved or rejected when the user responds to the consent request. This method also supports passing a callback instead of using a promise.
Explicitly raises the cookie widget. It may be helpful to bind this action to a link to your cookie settings, e.g.
metomic.withConsentFor(policyId,[cb]) : Promise
Will trigger a consent dialog for
policyId if the user has never opted in or out, calling back as appropriate with either success or failure.
If the user has previously made a choice on
policy-id in the past, the dialog will not be shown again - instead,
cb will be called back immediately with success or failure, depending on the user's choice.
If you wish to explicitly show a consent dialog, use requestConsentFor(...).
metomic.when(policy-id,[cb]) : Promise
Returns a promise that resolves if or when policy is consented to, and is rejected otherwise. This method also supports passing a callback instead of using a promise.