UNPKG

5.13 kBJavaScriptView Raw
1import Cookies from 'js-cookie'
2import repeat from "lodash-es/repeat"
3import gaCookieDomain from "./helpers/gaCookieDomain"
4import forEach from "lodash-es/forEach"
5import isUndefined from "lodash-es/isUndefined"
6import isNull from "lodash-es/IsNull"
7import isFunction from "lodash-es/isFunction"
8import isString from "lodash-es/isString"
9import isArray from "lodash-es/isArray"
10import sendHitTask from "./sendHitTask"
11import MeasurementFramework from "./MeasurementFramework"
12import {charCodeToValue, valueToChar} from "./helpers/charCodeCounter"
13import reverse from "lodash-es/reverse"
14
15
16const cookieName = "rmf"
17let frameworkEvents = []
18let frameworkStages = []
19let cookieValue
20
21export 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
30export 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
41export const getEventIndex = function (eventActionName) {
42 return frameworkEvents.indexOf(eventActionName)
43}
44
45export 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
57const eventExists = function (eventActionName) {
58 return (getEventIndex(eventActionName) !== -1)
59}
60
61export const greater = function (action, than) {
62 return getEventCount(action) > than
63}
64const setCookieValue = function (newCookieValue) {
65 cookieValue = newCookieValue
66 /**
67 * Setting Cookie
68 */
69 console.log("Setting Cookie " + cookieName + ":", newCookieValue)
70 Cookies.set(cookieName, newCookieValue, {
71 expires: 365,
72 domain: gaCookieDomain()
73 })
74}
75export const incrementEventValue = function (eventActionName) {
76 if (eventExists(eventActionName)) {
77 let eventCount = getEventCount(eventActionName)
78 if (eventCount < 62) {
79 /**
80 * Increment and transform value
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
99const 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 * Module that takes an array of events and an array of stage functions and
112 * calculate the user funnel stage.
113 *
114 * @param events
115 * @param stages
116 * @returns {boolean}
117 */
118export 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 * Register A standard callback on the custom event registered in the measurement framework
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 * Register some initial values
155 */
156 MeasurementFramework.register(function () {
157 return {
158 "cookieDomain": gaCookieDomain,
159 "userTrail": getEventMatrix,
160 "funnelStage": getUserFunnelStage
161 }
162 })
163}