concept Global { match '*' val url = `window.location.href` val pageName = `getPageName()` plugin script `console.log( 'Web analytics example' )` flow => debug plugin debug } def getPageName = `(location.pathname||'/').substring(1)||'home'`
In the next example creates a unique ID for visitors. This allows us to tie a visitor to the pages that were viewed (and sent to the server). A visitor ID is a random value that is stored in a cookie, so it is still available if the visitor returns within 30 days.
concept Global { match '*' val url = `window.location.href` val pageName = `getPageName()` val visitorId = `localStorage.dimmlcid=localStorage.dimmlcid||guid()` val sandboxID = `dimml.cookies.get('dimml')` val currentTime = `getCurrentTime()` val currentDate = `getCurrentDate()` plugin script `console.log( 'Web analytics example2' )` flow => debug plugin debug } def guid = `dimml.sha1(+new Date()+Math.random().toString(36).slice(2)+navigator.userAgent).slice(20)` def getPageName = `(location.pathname||'/').substring(1)||'home'` def getCurrentTime = ` var d = new Date(), h = (d.getHours()<10?'0':'') + d.getHours(), m = (d.getMinutes()<10?'0':'') + d.getMinutes(); return h + ':' + m; ` def getCurrentDate = ` var d = new Date(), dd = d.getDate(), mm = d.getMonth()+1, yyyy = d.getFullYear(); return dd + '-' + mm + '-' + yyyy; `
After adding the request plugin, the user agent string is available as a parameter. The user-agent flow element can then be used to process the string into several properties like browser and device type. This flow element is added to the flow as well before making the output available as a tuple.
Details on the request plugin can be here, details on the user agent flow element can be found here. This mechanism for getting the device information provides several benefits for developers:
The resulting code looks like this:
concept Global { match '*' val url = `window.location.href` val pageName = `getPageName()` val visitorId = `localStorage.dimmlcid=localStorage.dimmlcid||guid()` val sandboxID = `dimml.cookies.get('dimml')` val currentTime = `getCurrentTime()` val currentDate = `getCurrentDate()` plugin script `console.log( 'Web analytics example2' )` plugin request[ua = `headers['User-Agent']`] flow => useragent[parsedUserAgent = 'ua'] => debug plugin debug } def guid = `dimml.sha1(+new Date()+Math.random().toString(36).slice(2)+navigator.userAgent).slice(20)` def getPageName = `(location.pathname||'/').substring(1)||'home'` def getCurrentTime = ` var d = new Date(), h = (d.getHours()<10?'0':'') + d.getHours(), m = (d.getMinutes()<10?'0':'') + d.getMinutes(); return h + ':' + m; ` def getCurrentDate = ` var d = new Date(), dd = d.getDate(), mm = d.getMonth()+1, yyyy = d.getFullYear(); return dd + '-' + mm + '-' + yyyy; `