Skip to content

Commit a3fe6a7

Browse files
committed
set user and device info header in search request
set user and device info header in search request and accept additional headers info through search configuration
1 parent 072f16b commit a3fe6a7

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

unbxdSearch.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
481481
,searchQueryParam:"q"
482482
,retainbaseParam: false
483483
,baseParams:[]
484+
,requestHeaders: {}
484485
};
485486

486487
jQuery.extend(Unbxd.setSearch.prototype,{
@@ -1159,7 +1160,8 @@ var unbxdSearchInit = function(jQuery, Handlebars){
11591160

11601161
modifiedCB(data);
11611162
}
1162-
,urlobj = self.url();
1163+
,urlobj = self.url()
1164+
,requestHeaders = jQuery.extend({}, this.getDefaultRequestHeaders(), this.options.requestHeaders);
11631165

11641166
if(doPush){
11651167
var finalquery = this.options.noEncoding ? urlobj.query : this.encode( urlobj.query );
@@ -1185,6 +1187,7 @@ var unbxdSearchInit = function(jQuery, Handlebars){
11851187
,dataType: "jsonp"
11861188
,jsonp: 'json.wrf'
11871189
,success: cb.bind(self)
1190+
,headers: requestHeaders
11881191
});
11891192
}
11901193
,reset: function(){
@@ -1929,6 +1932,37 @@ var unbxdSearchInit = function(jQuery, Handlebars){
19291932

19301933
return undefined;
19311934
}
1935+
,getDeviceInfo : function(){
1936+
var smallDeviceMaxWidth = 768,
1937+
mediumDeviceMaxWidth = 992;
1938+
if(window.outerWidth < smallDeviceMaxWidth){
1939+
return "Mobile";
1940+
} else if(window.outerWidth < mediumDeviceMaxWidth){
1941+
return "Tablet";
1942+
} else {
1943+
return "Desktop";
1944+
}
1945+
}
1946+
,getUserType : function(){
1947+
return this.readCookie('visit') === "repeat" ? "repeat" : "new";
1948+
}
1949+
,getUserId : function(){
1950+
return this.readCookie('userId');
1951+
}
1952+
,getDefaultRequestHeaders : function(){
1953+
var self = this,
1954+
userId = this.getUserId(),
1955+
defaultRequestHeaders = {
1956+
"Device-Type": self.getDeviceInfo()
1957+
,"Unbxd-Url": document.URL
1958+
,"Unbxd-Referrer": document.referrer
1959+
,"User-Type": self.getUserType()
1960+
};
1961+
if(userId){
1962+
defaultRequestHeaders["User-Id"] = userId;
1963+
}
1964+
return defaultRequestHeaders;
1965+
}
19321966
});
19331967
};
19341968

0 commit comments

Comments
 (0)