First, we will need to add Daton template snippet file in your frontend.
Add the following script tag in your website:
<script type="text/javascript" src="https://unpkg.com/daton-template-snippet@latest/dist/daton-template-snippet.js"></script>
The script injects a global Daton variable in the window object.
Initiate the Daton template API:
Then, on any HTML template, on either click of a button or any other event, you can initiate the Daton template API.
<!DOCTYPE html>
<html>
<head>
<title>Sample HTML Template</title>
</head>
<body>
<p><button onclick="loadDaton()">Load Daton!</button></p>
</body>
</html>
<script type="text/javascript" src="https://unpkg.com/daton-template-snippet@latest/dist/daton-template-snippet.js"></script>
<script type="text/javascript">
function loadDaton() {
window.Daton.init({
apiKey: "<API_KEY>",
baseSourceName: "<SOURCE_NAME>",
newSourceNamePrefix: "<PREFIX>"
});
}
</script>
You can also pass onSuccess and onError callbacks to the init function.
function loadDaton() {
window.Daton.init({
apiKey: "<API_KEY>",
baseSourceName: "<SOURCE_NAME>",
newSourceNamePrefix: "<PREFIX>",
onSuccess: () => {
console.log("success")
},
onError: (err) => {
console.log("error")
}
});
}
To run the project locally, run the following command:
npm run start
To build the project, run the following command:
npm run build
To publish the package, run the following command:
- Update the version in package.json
- Run the following command:
npm publish --access public
- Go to https://www.npmjs.com/package/daton-template-snippet and check if the package is published successfully.