1 | import Cookies from 'js-cookie'
|
2 | import repeat from "lodash-es/repeat"
|
3 | import gaCookieDomain from "./helpers/gaCookieDomain"
|
4 | import forEach from "lodash-es/forEach"
|
5 | import isUndefined from "lodash-es/isUndefined"
|
6 | import isNull from "lodash-es/IsNull"
|
7 | import isFunction from "lodash-es/isFunction"
|
8 | import isString from "lodash-es/isString"
|
9 | import isArray from "lodash-es/isArray"
|
10 | import sendHitTask from "./sendHitTask"
|
11 | import MeasurementFramework from "./MeasurementFramework"
|
12 | import {charCodeToValue, valueToChar} from "./helpers/charCodeCounter"
|
13 | import reverse from "lodash-es/reverse"
|
14 |
|
15 |
|
16 | const cookieName = "rmf"
|
17 | let frameworkEvents = []
|
18 | let frameworkStages = []
|
19 | let cookieValue
|
20 |
|
21 | export const loadCookieValue = function () {
|
22 | cookieValue = Cookies.get(cookieName)
|
23 | if (isUndefined(cookieValue) || isNull(cookieValue)) {
|
24 | cookieValue = repeat("0", 64)
|
25 | setCookieValue(cookieValue)
|
26 | }
|
27 | return cookieValue
|
28 | }
|
29 |
|
30 | export const getEventMatrix = function () {
|
31 | cookieValue = loadCookieValue()
|
32 | let matrix = {}
|
33 | forEach(frameworkEvents, function (value, index) {
|
34 | let safeEventName = value.replace(/\s+/g, '')
|
35 | let charCode = cookieValue.charCodeAt(index)
|
36 | matrix[safeEventName] = charCodeToValue(charCode)
|
37 | })
|
38 | return matrix
|
39 | }
|
40 |
|
41 | export const getEventIndex = function (eventActionName) {
|
42 | return frameworkEvents.indexOf(eventActionName)
|
43 | }
|
44 |
|
45 | export const getEventCount = function (eventActionName) {
|
46 | let eventIndex = getEventIndex(eventActionName)
|
47 | if (eventIndex === -1) {
|
48 | return 0
|
49 | } else {
|
50 | cookieValue = loadCookieValue()
|
51 | let charCode = cookieValue.charCodeAt(eventIndex)
|
52 | let eventCount = charCodeToValue(charCode)
|
53 | return eventCount || 0
|
54 | }
|
55 | }
|
56 |
|
57 | const eventExists = function (eventActionName) {
|
58 | return (getEventIndex(eventActionName) !== -1)
|
59 | }
|
60 |
|
61 | export const greater = function (action, than) {
|
62 | return getEventCount(action) > than
|
63 | }
|
64 | const setCookieValue = function (newCookieValue) {
|
65 | cookieValue = newCookieValue
|
66 | |
67 |
|
68 |
|
69 | console.log("Setting Cookie " + cookieName + ":", newCookieValue)
|
70 | Cookies.set(cookieName, newCookieValue, {
|
71 | expires: 365,
|
72 | domain: gaCookieDomain()
|
73 | })
|
74 | }
|
75 | export const incrementEventValue = function (eventActionName) {
|
76 | if (eventExists(eventActionName)) {
|
77 | let eventCount = getEventCount(eventActionName)
|
78 | if (eventCount < 62) {
|
79 | |
80 |
|
81 |
|
82 | eventCount++
|
83 | console.log("Event Value Incremented:", eventActionName)
|
84 | let eventIndex = getEventIndex(eventActionName)
|
85 | let cookieSplitted = loadCookieValue().split("")
|
86 | cookieSplitted[eventIndex] = valueToChar(eventCount)
|
87 | let newCookieValue = cookieSplitted.join("")
|
88 | setCookieValue(newCookieValue)
|
89 | } else {
|
90 | console.log("Event Value reached max, doing nothing.", eventActionName)
|
91 | return false
|
92 | }
|
93 | return true
|
94 | } else {
|
95 | return false
|
96 | }
|
97 | }
|
98 |
|
99 | const getUserFunnelStage = function () {
|
100 | let stage
|
101 | forEach(frameworkStages, function (stageFunc) {
|
102 | stage = stageFunc()
|
103 | if (isString(stage)) {
|
104 | return false
|
105 | }
|
106 | })
|
107 | return stage
|
108 | }
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | export default function userTrail(events, stages) {
|
119 | if (isArray(events)) {
|
120 | frameworkEvents = events
|
121 | } else {
|
122 | console.warn("userTrail require the event config to be an Array.")
|
123 | return false
|
124 | }
|
125 | if (isArray(stages)) {
|
126 | frameworkStages = reverse(stages)
|
127 | } else {
|
128 | console.warn("userTrail require the stages config variable to be an Array.")
|
129 | return false
|
130 | }
|
131 | if (!forEach(stages, isFunction)) {
|
132 | console.warn("Wrong in stage configuration")
|
133 | return false
|
134 | }
|
135 |
|
136 | |
137 |
|
138 |
|
139 | sendHitTask(function (model) {
|
140 | let previousFunnelStage = getUserFunnelStage()
|
141 | let dlObj = {}
|
142 | forEach(["eventAction", "eventCategory", "eventLabel", "eventValue"], function (value) {
|
143 | dlObj[value] = model.get(value)
|
144 | })
|
145 | dlObj.userFunnelStagePrevious = previousFunnelStage
|
146 | dlObj.isStandardEvent = (incrementEventValue(model.get("eventAction")))
|
147 | let newFunnelStage = getUserFunnelStage()
|
148 | dlObj.funnelChangeLabel = previousFunnelStage + ' > ' + newFunnelStage
|
149 | dlObj.funnelStageChanged = (dlObj.isStandardEvent && (previousFunnelStage !== newFunnelStage))
|
150 | dlObj.funnelStage = newFunnelStage
|
151 | return dlObj
|
152 | })
|
153 | |
154 |
|
155 |
|
156 | MeasurementFramework.register(function () {
|
157 | return {
|
158 | "cookieDomain": gaCookieDomain,
|
159 | "userTrail": getEventMatrix,
|
160 | "funnelStage": getUserFunnelStage
|
161 | }
|
162 | })
|
163 | }
|