Dittofeed’s Web SDK allows you to send events from your web app to Dittofeed’s API.
Installation
yarn add @dittofeed/sdk-web
npm install --save @dittofeed/sdk-web
Usage
Dittofeed’s web sdk can be useful for sending Dittofeed events about your application and users.
import { DittofeedSdk } from '@dittofeed/sdk-web';
await DittofeedSdk.init({
writeKey: "Basic abcdefg...",
});
DittofeedSdk.identify({
userId: "123",
traits: {
email: "[email protected]",
firstName: "John"
},
});
DittofeedSdk.track({
userId: "123",
event: "Made Purchase",
properties: {
itemId: "abc",
},
});
DittofeedSdk.screen({
userId: "123",
name: "Recipe Screen",
properties: {
recipeType: "Soup",
},
});
await DittofeedSdk.flush();
Install the Snippet
You can also import the Dittofeed snippet into your web app. This snippet will automatically load the Dittofeed SDK and initialize it with your write key.
This is convenient if you want to use the Dittofeed SDK without a build system e.g. in a site builder like Webflow.
<script type="text/javascript">
var _df = _df || [];
(function () {
var methods = ["track", "identify", "page", "flush"];
methods.forEach(function (method) {
_df[method] = function () {
_df.push([method].concat(Array.prototype.slice.call(arguments)));
};
});
var script = document.createElement("script");
script.type = "module";
script.async = true;
// If you're self-hosting the Dittofeed dashboard, you'll need to to
// specificy your own host.
script.src = "https://app.dittofeed.com/dashboard/public/dittofeed.umd.js";
script.id = "df-tracker";
// Replace with your own write key found on: https://app.dittofeed.com/dashboard/dittofeed.umd.js
script.setAttribute("data-write-key", "Basic my-write-key");
// If you're self-hosting dittofeed, you'll need to to specificy your own host.
// script.setAttribute("data-host", "http://localhost:3001");
document.head.appendChild(script);
})();
_df.identify({
userId: "123",
email: "[email protected]"
});
</script>