UNPKG

3.72 kBJavaScriptView Raw
1import window from "window"
2import document from "document"
3import isString from "lodash-es/isString"
4import now from "lodash-es/now"
5import uniq from "lodash-es/uniq"
6import Cookies from "js-cookie"
7import jsonp from "jsonp-es6"
8import random from "alphanumeric"
9import dataLayerPush from "./helpers/dataLayerPush"
10import gaLoaded from "./helpers/gaExists"
11import fbqLoaded from "./helpers/fbqExists"
12import forEach from "lodash-es/forEach"
13import gaCookieDomain from "./helpers/gaCookieDomain"
14
15
16function kudakeru(kudakeruCallback) {
17 const cookieName = "kudakeru"
18 const sessionTimeout = 10
19
20 // just an utility function to shorten the time calculation.
21 let timestamp = function () {
22 return Math.round(now() / 1000)
23 }
24
25 let getTrackingUrl = function () {
26 if (window.__kudakeru_test) {
27 return "/" + cookieName
28 } else {
29 return "https://collect-dot-rpsleads.appspot.com/" + cookieName
30 }
31 }
32
33 // Wrapper for the cookie setMethod
34 let upsertCookie = function (kid) {
35 console.log("Upserting Cookie:", kid)
36 Cookies.set(cookieName, kid + "." + timestamp(), {
37 expires: 365,
38 domain: gaCookieDomain()
39 })
40 }
41
42 // Function that sends data back to the Katana service.
43 let sendData = function (queryData) {
44 console.log("Sending data to server:", queryData)
45 jsonp(getTrackingUrl(), queryData).then(function (responseData) {
46 console.log("Received data from server:", responseData)
47 if (!responseData.kid) {
48 console.log("Server is broken: ", trackingUrl)
49 dataLayerPush(cookieName + "Error", {"error": responseData})
50 return false
51 }
52 // Only when the server is responsing a cookie is updated and written.
53 upsertCookie(responseData.kid)
54 kudakeruCallback(responseData)
55 dataLayerPush(cookieName + "Loaded", responseData)
56 if (responseData.fbel && window.fbq) {
57 window.fbq("trackCustom", responseData.fbel)
58 }
59 }).catch(function (ex) {
60 dataLayerPush("Error", {"error": ex})
61 })
62 }
63 setTimeout(function () {
64 let queryData = {
65 "tid": [],
66 "cid": "",
67 "fb": [],
68 "ot": 0,
69 "dl": document.location.hostname + document.location.pathname
70 }
71
72 // Checking if GA object exists and collect data from it.
73 if (gaLoaded()) {
74 forEach(window.ga.getAll(), function (tracker) {
75 queryData.cid = tracker.get("clientId")
76 queryData.tid.push(tracker.get("trackingId"))
77 })
78 }
79
80 // Checking if FBQ exitsts
81 if (fbqLoaded()) {
82 forEach(window.fbq.getState().pixels, function (pixel) {
83 queryData.fb.push(pixel.id)
84 })
85 }
86
87 // Checking if the Cookie exists
88 let oldCookie = Cookies.get(cookieName)
89 if (isString(oldCookie) && oldCookie.split(".").length === 2) {
90 queryData.ot = oldCookie.split(".")[1]
91 queryData.kid = oldCookie.split(".")[0]
92 } else {
93 queryData.kid = random(12)
94 }
95
96 // Sending data to the server only if its more than 30 minutes since last hit
97 console.log("Kudakeru Query Data", queryData)
98 if (timestamp() - queryData.ot > sessionTimeout) {
99 queryData.tid = uniq(queryData.tid).join(",")
100 queryData.fb = queryData.fb.join(",")
101 sendData(queryData)
102 } else {
103 upsertCookie(queryData.kid)
104 }
105 }, 5000)
106}
107
108export default kudakeru
\No newline at end of file