1import { Controller } from '@hotwired/stimulus' 2 3// Connects to data-controller="google-analytics" 4export default class extends Controller { 5 static values = { event: String, unique: String, ecommerce: Object, data: Object } 6 7 connect () { 8 if (this.uniqueValue) { 9 if (this.readCookie(this.uniqueValue) === 'true') { 10 return 11 } else { 12 this.createCookie(this.uniqueValue, 'true', 7) 13 } 14 } 15 16 window.dataLayer ||= [] 17 18 const obj = { 19 event: this.eventValue 20 } 21 if (this.hasEcommerceValue) { 22 obj.ecommerce = this.ecommerceValue 23 } 24 window.dataLayer.push(obj) 25 } 26 27 createCookie (name, value, days) { 28 let expires = '' 29 if (days) { 30 const date = new Date() 31 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)) 32 expires = '; expires=' + date.toGMTString() 33 } 34 35 document.cookie = name + '=' + value + expires + '; path=/' 36 } 37 38 readCookie (name) { 39 const nameEQ = name + '=' 40 const ca = document.cookie.split(';') 41 for (let i = 0; i < ca.length; i++) { 42 let c = ca[i] 43 while (c.charAt(0) === ' ') c = c.substring(1, c.length) 44 if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length) 45 } 46 return null 47 } 48};
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.