loevgaard /
last-click
| 1 | "use strict"; |
||
| 2 | |||
| 3 | module.exports = create; |
||
| 4 | |||
| 5 | const cookie = require('cookie'); |
||
| 6 | const queryString = require('query-string'); |
||
| 7 | const merge = require('merge-objects'); |
||
| 8 | |||
| 9 | function create(document, settings) { |
||
| 10 | 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 | const location = document.location; |
||
| 25 | |||
| 26 | const obj = { |
||
| 27 | getLastClick: getLastClick, |
||
| 28 | check: check |
||
| 29 | }; |
||
| 30 | |||
| 31 | obj.check(); |
||
| 32 | |||
| 33 | return obj; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @param {String} value |
||
| 37 | */ |
||
| 38 | function save(value) { |
||
| 39 | const date = new Date(); |
||
| 40 | date.setTime(date.getTime() + (settings.cookie.expires * 1000)); |
||
| 41 | |||
| 42 | document.cookie = cookie.serialize(settings.cookie.name, value, { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 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 | const cookies = cookie.parse(document.cookie); |
||
|
0 ignored issues
–
show
|
|||
| 55 | if(hasProperty(cookies, settings.cookie.name)) { |
||
| 56 | return cookies[settings.cookie.name]; |
||
| 57 | } |
||
| 58 | |||
| 59 | return null; |
||
| 60 | } |
||
| 61 | |||
| 62 | function check() { |
||
| 63 | const queryParams = queryString.parse(location.search); |
||
| 64 | settings.sources.forEach(function (source) { |
||
| 65 | let val = ''; |
||
| 66 | |||
| 67 | if(hasProperty(source, 'queryParameter') && hasProperty(queryParams, source.queryParameter)) { |
||
| 68 | val = queryParams[source.queryParameter]; |
||
| 69 | } else if(hasProperty(source, 'referrer') && document.referrer && document.referrer.indexOf(source.referrer) >= 0) { |
||
|
0 ignored issues
–
show
|
|||
| 70 | val = document.referrer; |
||
| 71 | } |
||
| 72 | |||
| 73 | if(val) { |
||
| 74 | if(hasProperty(source, 'value')) { |
||
| 75 | if(typeof(source.value) === 'function') { |
||
| 76 | val = source.value.apply(val); |
||
| 77 | } else { |
||
| 78 | val = source.value; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | save(val); |
||
| 84 | } |
||
| 85 | }); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * |
||
| 90 | * @param {Object} obj |
||
| 91 | * @param {String} property |
||
| 92 | * @return {boolean} |
||
| 93 | */ |
||
| 94 | function hasProperty(obj, property) { |
||
| 95 | return Object.keys(obj).indexOf(property) !== -1 |
||
| 96 | } |
||
| 97 | } |