|
1
|
|
|
"use strict"; |
|
2
|
|
|
|
|
3
|
1 |
|
module.exports = create; |
|
4
|
|
|
|
|
5
|
1 |
|
const cookie = require('cookie'); |
|
6
|
1 |
|
const queryString = require('query-string'); |
|
7
|
1 |
|
const merge = require('merge-objects'); |
|
8
|
|
|
|
|
9
|
|
|
function create(document, settings) { |
|
10
|
2 |
|
settings = merge(settings || {}, { |
|
11
|
|
|
sources: [ |
|
12
|
|
|
{referrer: 'google'}, |
|
13
|
|
|
{referrer: 'yahoo'}, |
|
14
|
|
|
{referrer: 'bing'}, |
|
15
|
|
|
{queryParameter: 'gclid', 'value': 'adwords'}, |
|
16
|
|
|
{queryParameter: 'utm_source'} |
|
17
|
|
|
], |
|
18
|
|
|
cookie: { |
|
19
|
|
|
name: 'last_click', |
|
20
|
|
|
expires: 30 * 24 * 60 * 60 // 30 days in seconds |
|
21
|
|
|
} |
|
22
|
|
|
}); |
|
23
|
|
|
|
|
24
|
2 |
|
const location = document.location; |
|
25
|
|
|
|
|
26
|
2 |
|
const obj = { |
|
27
|
|
|
getLastClick: getLastClick, |
|
28
|
|
|
check: check |
|
29
|
|
|
}; |
|
30
|
|
|
|
|
31
|
2 |
|
obj.check(); |
|
32
|
|
|
|
|
33
|
2 |
|
return obj; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param {String} value |
|
37
|
|
|
*/ |
|
38
|
|
|
function save(value) { |
|
39
|
4 |
|
const date = new Date(); |
|
40
|
4 |
|
date.setTime(date.getTime() + (settings.cookie.expires * 1000)); |
|
41
|
|
|
|
|
42
|
4 |
|
document.cookie = cookie.serialize(settings.cookie.name, value, { |
|
|
|
|
|
|
43
|
|
|
httpOnly: true, |
|
44
|
|
|
maxAge: settings.cookie.expires, |
|
45
|
|
|
expires: date, |
|
46
|
|
|
path: '/' |
|
47
|
|
|
}); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return {String|*} |
|
52
|
|
|
*/ |
|
53
|
|
|
function getLastClick() { |
|
54
|
2 |
|
const cookies = cookie.parse(document.cookie); |
|
|
|
|
|
|
55
|
2 |
|
if(hasProperty(cookies, settings.cookie.name)) { |
|
56
|
2 |
|
return cookies[settings.cookie.name]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return null; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
function check() { |
|
63
|
4 |
|
const queryParams = queryString.parse(location.search); |
|
64
|
4 |
|
settings.sources.forEach(function (source) { |
|
65
|
20 |
|
let val = ''; |
|
66
|
|
|
|
|
67
|
20 |
|
if(hasProperty(source, 'queryParameter') && hasProperty(queryParams, source.queryParameter)) { |
|
68
|
2 |
|
val = queryParams[source.queryParameter]; |
|
69
|
18 |
|
} else if(hasProperty(source, 'referrer') && document.referrer && document.referrer.indexOf(source.referrer) >= 0) { |
|
|
|
|
|
|
70
|
2 |
|
val = document.referrer; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
20 |
|
if(val) { |
|
74
|
4 |
|
if(hasProperty(source, 'value')) { |
|
75
|
2 |
|
if(typeof(source.value) === 'function') { |
|
76
|
|
|
val = source.value.apply(val); |
|
77
|
|
|
} else { |
|
78
|
|
|
val = source.value; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
4 |
|
save(val); |
|
83
|
|
|
} |
|
84
|
|
|
}); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param {Object} obj |
|
89
|
|
|
* @param {String} property |
|
90
|
|
|
* @return {boolean} |
|
91
|
|
|
*/ |
|
92
|
|
|
function hasProperty(obj, property) { |
|
93
|
52 |
|
return Object.keys(obj).indexOf(property) !== -1 |
|
94
|
|
|
} |
|
95
|
|
|
} |