if (!window.AS) AS = {}; if (!window.autosweetGA) autosweetGA = {}; AS.pixel = { bodyText: null, doAnalyticsWork: function () { AS.pixel.loadGoogleAnalytics4API("G-9PLT0C9109"); }, doPixelWork: function () { if (document.readyState != "complete") { setTimeout(function () { AS.pixel.doPixelWork(); }, 250); } else { AS.pixel.bodyText = AS.pixel.getText(); AS.pixel.loadFacebookAPI(); AS.pixel.doFacebookWork(2036, "712374596032935", "VIN"); } }, loadGoogleAnalytics4API: function (gaTrackingCode) { var gt = document.createElement("script"); gt.async = "async"; gt.src = "https://www.googletagmanager.com/gtag/js?id=" + gaTrackingCode; document.head.appendChild(gt); window.dataLayer = window.dataLayer || []; autosweetGA[gaTrackingCode] = function () {dataLayer.push(arguments);}; autosweetGA[gaTrackingCode]('js', new Date()); autosweetGA[gaTrackingCode]('config', gaTrackingCode); }, loadFacebookAPI: function () { !function (f, b, e, v, n, t, s) { if (f.fbq) return; n = f.fbq = function () { n.callMethod ? n.callMethod.apply(n, arguments) : n.queue.push(arguments); }; if (!f._fbq) f._fbq = n; n.push = n; n.loaded = !0; n.version = '2.0'; n.queue = []; t = b.createElement(e); t.async = !0; t.src = v; s = b.getElementsByTagName(e)[0]; s.parentNode.insertBefore(t, s); }(window, document, 'script', 'https://connect.facebook.net/en_US/fbevents.js'); }, doFacebookWork: function (dealerID, pixelID, productIdType) { if (!window.fbq) { setTimeout(function () { AS.pixel.doFacebookWork(dealerID, pixelID, productIdType); }, 250); } else { AS.pixel.connectFB(pixelID, productIdType); } }, connectFB: function (pixelID, productIdType) { if (window.fbq) { window.fbq('init', pixelID); window.fbq('track', 'PageView'); AS.pixel.analyzePage(pixelID, productIdType); } }, analyzePage: function (pixelID, productIdType) { var pIds = productIdType == "VIN" ? AS.pixel.getPageVins() : AS.pixel.getPageStockNumbers(); if (pIds && pIds.length) { window.fbq("trackSingle", pixelID, "ViewContent", { content_type: "product", content_ids: pIds }); window.fbq("trackSingle", pixelID, "ViewContent", { content_type: "vehicle", content_ids: pIds }); } }, getPageVins: function () { var retVins = []; var vRe = /\b[a-hj-npr-z0-9]{17}\b/gi; for (var p = 0, props = [AS.pixel.bodyText, document.body['innerHTML']]; p < props.length; p++) { if (!retVins.length) { var dit = props[p]; var curVin; do { curVin = vRe.exec(dit); if (curVin && curVin.length) { curVin = curVin[0]; var wasFound = false; for (var i = 0; i < retVins.length; i++) { if (retVins[i].toUpperCase() == curVin.toUpperCase()) { wasFound = true; break; } } if (!wasFound && curVin.match(/[a-z]/i) && !curVin.substr(12,5).match(/[a-z]/i)) retVins.push(curVin.toUpperCase()); } } while (curVin != null); } } return retVins; }, getPageStockNumbers: function () { var retStockNumbers = []; var vRe = /(St(o?c)?k\s*|ID)(?:ID|#|N(?:o[\.]?|um(?:ber)?))?[:-\s]\s*([\w-]*)/gi; for (var p = 0, props = [AS.pixel.bodyText, document.body['innerHTML']]; p < props.length; p++) { if (!retStockNumbers.length) { var dit = props[p]; var curStockNumber; do { curStockNumber = vRe.exec(dit); if (curStockNumber && curStockNumber.length > 1) { curStockNumber = curStockNumber[curStockNumber.length - 1]; var wasFound = false; for (var i = 0; i < retStockNumbers.length; i++) { if (retStockNumbers[i] == curStockNumber) { wasFound = true; break; } } if (!wasFound) retStockNumbers.push(curStockNumber); } } while (curStockNumber != null); } } return retStockNumbers; }, getText: function () { var textNodes = AS.pixel.getTextNodesIn(document.body); var textValues = []; for (var i = textNodes.length - 1; i >= 0 ; i--) { if (textNodes[i]) { textValues.push(textNodes[i].textContent); } } return textValues.join(" "); }, getTextNodesIn: function (elem) { var textNodes = []; if (elem) { for (var nodes = elem.childNodes, i = nodes.length; i--;) { var node = nodes[i], nodeType = node.nodeType; if (nodeType == 3) { textNodes.push(node); } else if (nodeType == 1 || nodeType == 9 || nodeType == 11) { textNodes = textNodes.concat(AS.pixel.getTextNodesIn(node)); } } } return textNodes; } }; AS.pixel.doAnalyticsWork(); AS.pixel.doPixelWork();