Documentation Index
Fetch the complete documentation index at: https://docs.trench.dev/llms.txt
Use this file to discover all available pages before exploring further.
Overview
trench-js is a client library for both web and Node.js environments that allows you to send events to any Trench instance. This library is designed to be easy to use and integrate into your existing projects. The library is built on top of Analytics and supports all of its methods. It can be installed via a package manager or via a CDN script tag.
Installation via package manager
Install trench-js using your preferred package manager:
You can now initialize the client with your Trench instance URL and public API key:
import Trench from 'trench-js'
const trench = new Trench({
serverUrl: 'your-server-url',
publicApiKey: 'your-public-api-key',
})
For example, you can identify a user like this:
trench.identify('user-id', {
email: 'test@example.com',
})
And track an event like this:
trench.track('test-event', {
test: 'test-value',
})
Or to record a trigger page view:
Installation via CDN script tag
Alternatively, you can use the CDN version:
<script>
(function(d,w){if(!w.Trench){w.Trench=function(c){var i={_config:c,_q:[]},m=["track","page","identify","group","getEvents","executeQueries"];m.forEach(function(f){i[f]=function(){i._q.push([f,arguments])}});return i;};
window.trench=new Trench({
publicApiKey: 'YOUR_PUBLIC_API_KEY',
serverUrl: 'YOUR_SERVER_URL',
autoCaptureEvents: true
});
var s=d.createElement("script");s.type="text/javascript";s.async=!0;s.src="https://cdn.jsdelivr.net/npm/trench-js@latest/dist/trench.min.js";
s.onload=function(){var r=new Trench(trench._config);trench._q.forEach(function(q){r[q[0]].apply(r,q[1])});w.trench=r;};
d.getElementsByTagName("head")[0].appendChild(s);}})(document,window);
</script>
This will automatically add the trench object to the global scope and you can start tracking events automatically as autoCaptureEvents is set to true by default.