Skip to content

Commit a7eaa83

Browse files
mmathias01mhadam
authored andcommitted
Consolidate request method API (close snowplow#700)
1 parent 36fa850 commit a7eaa83

2 files changed

Lines changed: 34 additions & 18 deletions

File tree

src/js/out_queue.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,29 @@
5151
* @param object mutSnowplowState Gives the pageUnloadGuard a reference to the outbound queue
5252
* so it can unload the page when all queues are empty
5353
* @param boolean useLocalStorage Whether to use localStorage at all
54-
* @param boolean usePost Whether to send events by POST or GET
54+
* @param string eventMethod if null will use 'beacon' otherwise can be set to 'post', 'get', or 'beacon' to force.
5555
* @param int bufferSize How many events to batch in localStorage before sending them all.
5656
* Only applies when sending POST requests and when localStorage is available.
5757
* @param int maxPostBytes Maximum combined size in bytes of the event JSONs in a POST request
5858
* @param string postPath The path where events are to be posted
5959
* @return object OutQueueManager instance
6060
*/
61-
object.OutQueueManager = function (functionName, namespace, mutSnowplowState, useLocalStorage, useBeacon, usePost, postPath, bufferSize, maxPostBytes) {
61+
object.OutQueueManager = function (functionName, namespace, mutSnowplowState, useLocalStorage, eventMethod, postPath, bufferSize, maxPostBytes) {
6262
var queueName,
6363
executingQueue = false,
6464
configCollectorUrl,
6565
outQueue;
66-
67-
useBeacon = useBeacon && navigator && navigator.sendBeacon;
68-
66+
67+
//Force to lower case if its a string
68+
eventMethod = eventMethod.toLowerCase ? eventMethod.toLowerCase() : eventMethod;
69+
70+
//Use the Beacon API if eventMethod is set null, true, or 'beacon'.
71+
var enableBeacon = (eventMethod === null || eventMethod === true || eventMethod === "beacon" || eventMethod === "true") ? true : false;
72+
// Fall back to POST or GET for browsers which don't support Beacon API
73+
useBeacon = enableBeacon && navigator && navigator.sendBeacon;
74+
75+
//Use POST if specified, or beacon is unavailable.
76+
var usePost = (eventMethod === "post" || (enableBeacon && !useBeacon)) ? true : false;
6977
// Fall back to GET for browsers which don't support CORS XMLHttpRequests (e.g. IE <= 9)
7078
usePost = usePost && window.XMLHttpRequest && ('withCredentials' in new XMLHttpRequest());
7179

src/js/tracker.js

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -76,21 +76,32 @@
7676
* 12. useCookies, true
7777
* 13. sessionCookieTimeout, 1800
7878
* 14. contexts, {}
79-
* 15. beacon, false
80-
* 16. post, false
81-
* 17. bufferSize, 1
82-
* 18. crossDomainLinker, false
83-
* 19. maxPostBytes, 40000
84-
* 20. discoverRootDomain, false
85-
* 21. cookieLifetime, 63072000
86-
* 22. stateStorageStrategy, 'cookieAndLocalStorage'
79+
* 15. eventMethod, 'beacon'
80+
* 16. post, false *DEPRECATED use eventMethod instead*
81+
* 17. postPath, null
82+
* 18. bufferSize, 1
83+
* 19. crossDomainLinker, false
84+
* 20. maxPostBytes, 40000
85+
* 21. discoverRootDomain, false
86+
* 22. cookieLifetime, 63072000
87+
* 23. stateStorageStrategy, 'cookieAndLocalStorage'
8788
*/
8889
object.Tracker = function Tracker(functionName, namespace, version, mutSnowplowState, argmap) {
8990

9091
/************************************************************
9192
* Private members
9293
************************************************************/
93-
var
94+
95+
var argmap = argmap || {};
96+
97+
//use POST if that property is present on the argmap
98+
if(argmap.hasOwnProperty('post')) {
99+
argmap.eventMethod = argmap.post === true ? 'post' : 'get';
100+
} else {
101+
argmap.eventMethod = argmap.eventMethod || 'beacon'
102+
}
103+
104+
var
94105
// Tracker core
95106
core = coreConstructor(true, function(payload) {
96107
addBrowserData(payload);
@@ -127,8 +138,6 @@
127138

128139
customReferrer,
129140

130-
argmap = argmap || {},
131-
132141
// Request method is always GET for Snowplow
133142
configRequestMethod = 'GET',
134143

@@ -297,8 +306,7 @@
297306
mutSnowplowState,
298307
configStateStorageStrategy == 'localStorage' ||
299308
configStateStorageStrategy == 'cookieAndLocalStorage',
300-
argmap.beacon,
301-
argmap.post,
309+
argmap.eventMethod,
302310
configPostPath,
303311
argmap.bufferSize,
304312
argmap.maxPostBytes || 40000),

0 commit comments

Comments
 (0)