brain-sam-js v1.0.8
sam.js API
js api:
<script type="text/javascript">
window.sam_data = []
sam_data.push({config: {sam_id: 'abcdef'})
</script>
<script src="https://cdn.jsdelivr.net/npm/brain-sam-js@0.0.3/dist/esbuild/sam.min.js"></script>
config options:
- **sam_id** - id of master sam
- **autoview** - true/false - trigger page view on load, default: true
- **observable** - undefined/location/function - trigger page view on each `observable` value change, default: undefined
- **disable_3rd_party_cookies: true** - disable 3rd party cookie on dep-x.com domain
- **disable_tracking_browser_data: true** - disable auto-tracking of browser data
data layer:
- any code on page can add extra data to internal data layer to send with sam pixel, if data will be present in data layer it will be added to all next events
- Example:
<script type="text/javascript"> sam_data.push({user: {zipcode: '12345'}) // puts data in internal data layer - it will be added to all events sent in current view session sam_data.push({event: 'order_completed', e_t: 'conversion'}) </script>
- Example2
Sending events: By default page view event is sent when sam.js is loaded, it can be changed with config value:
<script type="text/javascript">
window.sam_data = []
sam_data.push({config: {sam_id: 'safdsaf', autoview: false})
</script>
<script src="https://cdn.jsdelivr.net/npm/brain-sam-js@0.0.3/dist/esbuild/sam.min.js"></script>
// in other place of code - eg. when page data loading is completed:
<script type="text/javascript">
sam_data.push({p_t: 'Sport event', p_section: 'sport', p_l: '/sport/event-post'})
sam_data.push({event: 'pageview'})
</script>
Custom events: Developer can send any custom js triggered event, by putting it in sam_data layer, all data provided with event object will be included in pixel, but not stored in data layer for following events:
// order form is displayed to the user
<script type="text/javascript">
sam_data.push({event: 'order_form', e_item_type: 'car'})
</script>
Non-navigational events: By default all events are counted in total page views, if you want just send data/event without putting it in total counts use event_type parameter:
// order confirmed
<script type="text/javascript">
sam_data.push({event: 'order_completed', e_t: 'nonnavigational', e_amount: 122.22, e_currency: 'USD'})
</script>
Event plugins: any external code can register plugin that is listening to all incoming events and has ability to modify data before sending it to server:
<script type="text/javascript">
sam_data.push({plugin: function(event_id, event_object, data_layer) {
//this function will be called every time when event is about to send
if(event_object.event == 'order_completed') {
fetch_some_extra_data(event_object.order_id, function(data) {
sam_data.push(event_object.merge({e_id: event_id, e_order_status: data.status}))
// add extra data to event when available
})
}
// update data layer before sending event
total_amount = data_layer.get('s_total_amount') || 0.0
data_layer.set('s_total_amount', total_amount + e.amount)
// update event_data
event_object.e_date = fetchDate()
} })
</script>
Data layer listener: By registering data layer listener you can modify/expand input data before is included in data layer OR trigger custom events based on data layer values
<script type="text/javascript">
sam_data.push({processor: function(incoming_data, data_layer) {
if(incoming_data.amount) {
total_amount = data_layer.get('s_total_amount') || 0.0
return {total_amount: total_amount + incoming_data.amount}
// sam_data.push({amount: 123.00)} - data layer = {total_amount: 123.00}
// sam_data.push({amount: 12)} - data layer = {total_amount: 135.00}
}
// sam_data.push({other: 'abc')} - data layer = {total_amount: 135.00, other: 'abc'}
} })
</script>
pixel api:
endpoint:
data scopes:
- user prefix: u_
- device/domain: d_
- session: s_
- event: e_
- page/view/resource: p_
methods:
- /e.gif?n=SAM_ID&e=EVENT_NAME….
- domain cookie: buid=UUID
- headers stored/processed: client_ip, referrer, user_agent, accept_language
- n - SAM ID
- e - event name - default page_view
- e_id - event id - default nil - all data in events with the same id will be merged
- e_t - event type (navigational, nonnavigational, data, user matching) default: nonnavigational for events other then page_view - if event type is set to navigational it will be counted in total page views
- p_cid, click_id - click id.
- d_id - first party cookie: dep=UUID
- d_pid - publisher internal id, eg. pubcid
- p_d - page domain, default: window.location.host
- p_l - page location, default: window.location.href
- p_r - page referrer, default: document.referrer
- p_t - page title
- d_pr - device pixel ratio
- d_h - current sceen height, default: window.screen.height
- d_w - current sceen width, default: window.screen.height
- u_zipcode - user zipcode
- u_email - user email - this is sensitive data, please contact us to establish encryption method
- u_pnr - user pnr - this is sensitive data, please contact us to establish encryption method
- d_location - current browser location format: latitude,longitude
- s_campaign - campaign name/id for current user session
- p_section - page section
- e_amount - conversion/event value
- e_currency - conversion value currency, default: EUR
- other/custom params: any other parameter with proper prefix (u, d, s, p, e_) will be stored in db for future usage, supported data types:
- integer
- float
- string (max 100 chars) category/true/false etc
- array of strings: ‘abc’, ‘def’, ‘xyz’ (maximum 20 elements, each max 20 chars)
- date - YYYY-MM-DD
- time - HH:MM(:SS) eg. 23:12, 12:34:01
development
yarn build-all
- builds all scripts in /distyarn esbuild-browser:watch
- watches changes in sam.js code & builds browser version of jsyarn server
- runs web server with sam.js DEMOhttp://localhost:8080/browser-test.html
yarn test
- runs sam.js testsyarn docs
- builds documentationyarn publish
- publishes new version of sam.js