1 | import window from "window"
|
2 | import document from "document"
|
3 | import isString from "lodash-es/isString"
|
4 | import now from "lodash-es/now"
|
5 | import uniq from "lodash-es/uniq"
|
6 | import Cookies from "js-cookie"
|
7 | import jsonp from "jsonp-es6"
|
8 | import random from "alphanumeric"
|
9 | import dataLayerPush from "./helpers/dataLayerPush"
|
10 | import gaLoaded from "./helpers/gaExists"
|
11 | import fbqLoaded from "./helpers/fbqExists"
|
12 | import forEach from "lodash-es/forEach"
|
13 | import gaCookieDomain from "./helpers/gaCookieDomain"
|
14 |
|
15 |
|
16 | function kudakeru(kudakeruCallback) {
|
17 | const cookieName = "kudakeru"
|
18 | const sessionTimeout = 10
|
19 |
|
20 |
|
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 |
|
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 |
|
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 |
|
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 |
|
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 |
|
81 | if (fbqLoaded()) {
|
82 | forEach(window.fbq.getState().pixels, function (pixel) {
|
83 | queryData.fb.push(pixel.id)
|
84 | })
|
85 | }
|
86 |
|
87 |
|
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 |
|
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 |
|
108 | export default kudakeru |
\ | No newline at end of file |