Added configure method
Andrew Kane
committed Aug 23, 2016
commit 9a3dc7bdc790eba14e4cca8b630f410cd5b03a5b
Showing 1
changed file with
42 additions
and 11 deletions
vendor/assets/javascripts/ahoy.js
+42
-11
| @@ | @@ -11,7 +11,29 @@ |
| (function (window) { | |
| "use strict"; | |
| + | var config = { |
| + | urlPrefix: "", |
| + | visitsUrl: "/ahoy/visits", |
| + | eventsUrl: "/ahoy/events", |
| + | domain: null, |
| + | page: null, |
| + | platform: "Web", |
| + | trackNow: false |
| + | }; |
| + | |
| var ahoy = window.ahoy || window.Ahoy || {}; | |
| + | |
| + | ahoy.configure = function (options) { |
| + | for (var key in options) { |
| + | if (options.hasOwnProperty(key)) { |
| + | config[key] = options[key]; |
| + | } |
| + | } |
| + | }; |
| + | |
| + | // legacy |
| + | ahoy.configure(ahoy); |
| + | |
| var $ = window.jQuery || window.Zepto || window.$; | |
| var visitId, visitorId, track; | |
| var visitTtl = 4 * 60; // 4 hours | |
| @@ | @@ -20,9 +42,18 @@ |
| var queue = []; | |
| var canStringify = typeof(JSON) !== "undefined" && typeof(JSON.stringify) !== "undefined"; | |
| var eventQueue = []; | |
| - | var visitsUrl = ahoy.visitsUrl || "/ahoy/visits"; |
| - | var eventsUrl = ahoy.eventsUrl || "/ahoy/events"; |
| - | var canTrackNow = ahoy.trackNow && canStringify && typeof(window.navigator.sendBeacon) !== "undefined"; |
| + | |
| + | function visitsUrl() { |
| + | return config.urlPrefix + config.visitsUrl; |
| + | } |
| + | |
| + | function eventsUrl() { |
| + | return config.urlPrefix + config.eventsUrl; |
| + | } |
| + | |
| + | function canTrackNow() { |
| + | return config.trackNow && canStringify && typeof(window.navigator.sendBeacon) !== "undefined"; |
| + | } |
| // cookies | |
| @@ | @@ -35,8 +66,8 @@ |
| date.setTime(date.getTime() + (ttl * 60 * 1000)); | |
| expires = "; expires=" + date.toGMTString(); | |
| } | |
| - | if (ahoy.domain) { |
| - | cookieDomain = "; domain=" + ahoy.domain; |
| + | if (config.domain) { |
| + | cookieDomain = "; domain=" + config.domain; |
| } | |
| document.cookie = name + "=" + escape(value) + expires + cookieDomain + "; path=/"; | |
| } | |
| @@ | @@ -104,7 +135,7 @@ |
| if (canStringify) { | |
| $.ajax({ | |
| type: "POST", | |
| - | url: eventsUrl, |
| + | url: eventsUrl(), |
| data: JSON.stringify([event]), | |
| contentType: "application/json; charset=utf-8", | |
| dataType: "json", | |
| @@ | @@ -126,12 +157,12 @@ |
| function trackEventNow(event) { | |
| ready( function () { | |
| var payload = new Blob([JSON.stringify([event])], {type : "application/json; charset=utf-8"}); | |
| - | navigator.sendBeacon(eventsUrl, payload) |
| + | navigator.sendBeacon(eventsUrl(), payload); |
| }); | |
| } | |
| function page() { | |
| - | return ahoy.page || window.location.pathname; |
| + | return config.page || window.location.pathname; |
| } | |
| function eventProperties(e) { | |
| @@ | @@ -178,7 +209,7 @@ |
| var data = { | |
| visit_token: visitId, | |
| visitor_token: visitorId, | |
| - | platform: ahoy.platform || "Web", |
| + | platform: config.platform, |
| landing_page: window.location.href, | |
| screen_width: window.screen.width, | |
| screen_height: window.screen.height | |
| @@ | @@ -191,7 +222,7 @@ |
| log(data); | |
| - | $.post(visitsUrl, data, setReady, "json"); |
| + | $.post(visitsUrl(), data, setReady, "json"); |
| } else { | |
| log("Cookies disabled"); | |
| setReady(); | |
| @@ | @@ -240,7 +271,7 @@ |
| }; | |
| log(event); | |
| - | if (canTrackNow) { |
| + | if (canTrackNow()) { |
| trackEventNow(event); | |
| } else { | |
| eventQueue.push(event); | |