Skip to content

Commit

Permalink
Merge pull request #127 from divyadaglia-unbxd/promoted_suggestions
Browse files Browse the repository at this point in the history
feat(Autosuggest):Promoted Suggestions addition
  • Loading branch information
divyadaglia-unbxd authored Feb 7, 2020
2 parents b540c45 + b24e29f commit ec58e77
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 9 deletions.
15 changes: 15 additions & 0 deletions mocks/currentResults.json
Original file line number Diff line number Diff line change
Expand Up @@ -767,5 +767,20 @@
},
"source": "type"
}
],
"promotedSuggestions": [
{
"doctype": "PROMOTED_SUGGESTION",
"end_unbxdDate": 1893426360000,
"start_unbxdDate": 1577461560000,
"timeStamp_unbxd": 1580979109956,
"autosuggest": "tail boom t-rex 500 pro",
"autosuggest_unstemmed": "tail boom t-rex 500 pro",
"uniqueId": "PROMOTED_SUGGESTION `~|@` tail boom t-rex 500 pro",
"promoted_suggestion_priority_unbxd_double": 0,
"unbxdFeedId": "prd-amainhobbies-com806591559753334_-1144818779",
"_version_": 1657776755843268600,
"parent_unbxd": true
}
]
}
10 changes: 10 additions & 0 deletions tests/test_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ describe('Autosuggest', function () {
popularProducts: {
count: 5,
fields: []
},
promotedSuggestions: {
count: 2
}
}

Expand All @@ -56,11 +59,14 @@ describe('Autosuggest', function () {
// With Max Suggestions
window.autoSuggestObj.options = mockOptionsWithoutMaxSuggestions;
var autoSuggestUrl = window.autoSuggestObj.autosuggestUrl();
console.log(autoSuggestUrl);
var searchParams = new URLSearchParams(autoSuggestUrl);
expect(searchParams.get('inFields.count')).to.equal(mockOptionsWithoutMaxSuggestions.inFields.count.toString());
expect(searchParams.get('topQueries.count')).to.equal(mockOptionsWithoutMaxSuggestions.topQueries.count.toString());
expect(searchParams.get('keywordSuggestions.count')).to.equal(mockOptionsWithoutMaxSuggestions.keywordSuggestions.count.toString());
expect(searchParams.get('popularProducts.count')).to.equal(mockOptionsWithoutMaxSuggestions.popularProducts.count.toString());
expect(searchParams.get('promotedSuggestion.count')).to.equal(mockOptionsWithoutMaxSuggestions.promotedSuggestions.count.toString());


// Without Max Suggestions
window.autoSuggestObj.options = mockOptionsWithMaxSuggestions;
Expand All @@ -70,6 +76,8 @@ describe('Autosuggest', function () {
expect(searchParams.get('topQueries.count')).to.equal(mockOptionsWithMaxSuggestions.maxSuggestions.toString());
expect(searchParams.get('keywordSuggestions.count')).to.equal(mockOptionsWithMaxSuggestions.maxSuggestions.toString());
expect(searchParams.get('popularProducts.count')).to.equal(mockOptionsWithMaxSuggestions.maxSuggestions.toString());
expect(searchParams.get('promotedSuggestion.count')).to.equal(mockOptionsWithMaxSuggestions.maxSuggestions.toString());

});

it('Should have called Universal/Classical analytics', function () {
Expand Down Expand Up @@ -135,8 +143,10 @@ describe('Autosuggest', function () {
window.autoSuggestObj.currentResults.KEYWORD_SUGGESTION = this.mockCurrentResults.keywordSuggestions;
window.autoSuggestObj.currentResults.TOP_SEARCH_QUERIES = this.mockCurrentResults.topQueries;
window.autoSuggestObj.currentResults.IN_FIELD = this.mockCurrentResults.inFields;
window.autoSuggestObj.currentResults.PROMOTED_SUGGESTION = this.mockCurrentResults.promotedSuggestions;
window.autoSuggestObj.sortSuggestionsBylength();
var sortedSuggestions = window.autoSuggestObj.currentResults.SORTED_SUGGESTIONS;

var inFields = window.autoSuggestObj.currentResults.IN_FIELD;
var sorted = false;
var sortedInfields = false;
Expand Down
117 changes: 108 additions & 9 deletions unbxdAutosuggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
, sideContentOn: "right" //"left"
, template: "1column" // "2column"
, theme: "#ff8400"
, mainTpl: ['inFields', 'keywordSuggestions', 'topQueries', 'popularProducts']
, mainTpl: ['inFields', 'keywordSuggestions', 'topQueries', 'popularProducts', 'promotedSuggestions']
, sideTpl: []
, showCarts: true // will be used in default template of popular products
, cartType: "inline" // "separate" will be used in default template popular products
Expand Down Expand Up @@ -260,6 +260,10 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
, header: ""
, tpl: "{{{safestring highlighted}}}"
}
, promotedSuggestions: {
count: 3,
tpl: "{{{safestring highlighted}}}"
}
, suggestionsHeader: ''
, popularProducts: {
count: 2
Expand Down Expand Up @@ -791,7 +795,8 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
'POPULAR_PRODUCTS': 'Pop_Click',
'KEYWORD_SUGGESTION': 'TQ_Click',
'TOP_SEARCH_QUERIES': 'TQ_Click',
'POPULAR_PRODUCTS_FILTERED': 'Filtered_Pop_Click'
'POPULAR_PRODUCTS_FILTERED': 'Filtered_Pop_Click',
'PROMOTED_SUGGESTION': 'TQ_Click',
}
return types[autosuggestType];
}
Expand All @@ -806,7 +811,8 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
'POPULAR_PRODUCTS': value + '-' + index,
'KEYWORD_SUGGESTION': value + '-' + index,
'TOP_SEARCH_QUERIES': value + '-' + index,
'POPULAR_PRODUCTS_FILTERED': value + '-' + index
'POPULAR_PRODUCTS_FILTERED': value + '-' + index,
'PROMOTED_SUGGESTION': value + '-' + index
};
return types[params.autosuggest_type];
}
Expand Down Expand Up @@ -1071,13 +1077,15 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
+ '&topQueries.count=' + this.options.maxSuggestions
+ '&keywordSuggestions.count=' + this.options.maxSuggestions
+ '&popularProducts.count=' + this.options.popularProducts.count
+ '&promotedSuggestion.count=' + this.options.maxSuggestions
+ '&indent=off';
}
else {
url += '&inFields.count=' + this.options.inFields.count
+ '&topQueries.count=' + this.options.topQueries.count
+ '&keywordSuggestions.count=' + this.options.keywordSuggestions.count
+ '&popularProducts.count=' + this.options.popularProducts.count
+ '&promotedSuggestion.count=' + this.options.promotedSuggestions.count
+ '&indent=off';
}

Expand Down Expand Up @@ -1131,12 +1139,15 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
}
}
, max_suggest: function (data) {
var infield_result = 0, topquery_result = 0, keyword_result = 0;
var infield_result = 0, topquery_result = 0, keyword_result = 0, promoted_result = 0;
var infield_sugg = Math.floor(this.options.maxSuggestions * 0.2);
var keyword_sugg = Math.floor(this.options.maxSuggestions * 0.4);
var topquery_sugg = Math.ceil(this.options.maxSuggestions * 0.4);
var keyword_sugg = Math.floor(this.options.maxSuggestions * 0.3);
var topquery_sugg = Math.ceil(this.options.maxSuggestions * 0.3);
var promoted_sugg = Math.floor(this.options.maxSuggestions * 0.2);

var keyword_rem = 0,
topquery_rem = 0;
topquery_rem = 0,
promoted_rem = 0;
for (var x = 0; x < data.response.products.length; x++) {
if (data.response.products[x].doctype == "IN_FIELD") {
infield_result++;
Expand All @@ -1147,6 +1158,9 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
else if (data.response.products[x].doctype == "TOP_SEARCH_QUERIES") {
topquery_result++;
}
else if (data.response.products[x].doctype == "PROMOTED_SUGGESTION") {
promoted_result++;
}
}


Expand Down Expand Up @@ -1174,6 +1188,15 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
topquery_sugg = topquery_result;
}
}
else if (promoted_result > promoted_sugg) {
if ((promoted_result - promoted_sugg) >= infield_rem) {
promoted_sugg = promoted_sugg + infield_rem;
infield_rem = 0;
} else {
infield_rem = infield_rem - promoted_result + promoted_sugg;
promoted_sugg = promoted_result;
}
}
else
infield_rem = 0;

Expand All @@ -1193,6 +1216,14 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
topquery_rem = topquery_rem - keyword_result + keyword_sugg;
keyword_sugg = keyword_result;
}
} else if (promoted_result > promoted_sugg) {
if ((promoted_result - promoted_sugg) >= topquery_rem) {
promoted_sugg = promoted_sugg + topquery_rem;
topquery_rem = 0;
} else {
topquery_rem = topquery_rem - promoted_result + promoted_sugg;
promoted_sugg = promoted_result;
}
}
// else if(infield_result > infield_sugg){
// if((infield_result - infield_sugg) >= topquery_rem){
Expand Down Expand Up @@ -1226,6 +1257,14 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
keyword_rem = keyword_rem - topquery_result + topquery_sugg;
topquery_sugg = topquery_result;
}
} else if (promoted_result > promoted_sugg) {
if ((promoted_result - promoted_sugg) >= keyword_rem) {
promoted_sugg = promoted_sugg + keyword_rem;
keyword_rem = 0;
} else {
keyword_rem = keyword_rem - promoted_result + promoted_sugg;
promoted_sugg = promoted_result;
}
}
// else if(infield_result > infield_sugg){
// if((infield_result - infield_sugg) >= keyword_rem){
Expand All @@ -1247,12 +1286,39 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
// keyword_rem = 0;
// keyword_sugg = keyword_result;
// }

if (promoted_result < promoted_sugg) {
promoted_rem = promoted_sugg - promoted_result;
while (promoted_rem > 0) {
if (topquery_result > topquery_sugg) {
if ((topquery_result - topquery_sugg) >= promoted_rem) {
topquery_sugg = topquery_sugg + promoted_rem;
promoted_rem = 0;
} else {
promoted_rem = promoted_rem - topquery_result + topquery_sugg;
topquery_sugg = topquery_result;
}
} else if (keyword_result > keyword_sugg) {
if ((keyword_result - keyword_sugg) >= promoted_rem) {
keyword_sugg = keyword_sugg + promoted_rem;
promoted_rem = 0;
} else {
promoted_rem = promoted_rem - keyword_result + keyword_sugg;
keyword_sugg = keyword_result;
}
} else promoted_rem = 0;
}
promoted_sugg = promoted_result;
}

var count = {};
count['infields'] = infield_sugg;
count['topquery'] = topquery_sugg;
count['promoted'] = promoted_sugg;
count['keyword'] = keyword_sugg;
count['key_rem'] = keyword_rem;
count['top_rem'] = topquery_rem;
count['promo_rem'] = promoted_rem;
return count;

}
Expand Down Expand Up @@ -1422,6 +1488,16 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
};
this.currentResults.KEYWORD_SUGGESTION.push(o);
}
, processPromotedSuggestion: function (doc) {
var o = {
autosuggest: doc.autosuggest,
highlighted: this.highlightStr(doc.autosuggest),
type: "PROMOTED_SUGGESTION",
_original: doc,
source: doc.unbxdAutosuggestSrc || ""
};
this.currentResults.PROMOTED_SUGGESTION.push(o);
}
, setDefaultPopularProductsOptions: function () {
if (!this.options.popularProducts.autosuggestName) {
this.options.popularProducts.autosuggestName = 'title';
Expand Down Expand Up @@ -1603,7 +1679,7 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {

}
, sortSuggestionsBylength: function () {
this.currentResults.SORTED_SUGGESTIONS = this.currentResults.KEYWORD_SUGGESTION.concat(this.currentResults.TOP_SEARCH_QUERIES);
this.currentResults.SORTED_SUGGESTIONS = this.currentResults.KEYWORD_SUGGESTION.concat(this.currentResults.TOP_SEARCH_QUERIES, this.currentResults.PROMOTED_SUGGESTION);
this.currentResults.SORTED_SUGGESTIONS.sort(function (a, b) {
return customSort(a.autosuggest, b.autosuggest);
});
Expand All @@ -1623,6 +1699,7 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
, POPULAR_PRODUCTS: []
, IN_FIELD: []
, SORTED_SUGGESTIONS: []
, PROMOTED_SUGGESTION: []
}
var infieldsCount = 0;
var key_count = 0,
Expand Down Expand Up @@ -1656,6 +1733,9 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
} else if ("POPULAR_PRODUCTS" == doc.doctype
&& this.options.popularProducts.count > this.currentResults.POPULAR_PRODUCTS.length) {
this.processPopularProducts(doc);
} else if ("PROMOTED_SUGGESTION" == doc.doctype && count['promoted'] > this.currentResults.PROMOTED_SUGGESTION.length &&
this.isUnique(doc.autosuggest, uniqueSuggestions)) {
this.processPromotedSuggestion(doc);
}
} else {
if ("TOP_SEARCH_QUERIES" == doc.doctype && this.options.topQueries.count > this.currentResults.TOP_SEARCH_QUERIES.length
Expand All @@ -1671,6 +1751,10 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
} else if ("POPULAR_PRODUCTS" == doc.doctype
&& this.options.popularProducts.count > this.currentResults.POPULAR_PRODUCTS.length) {
this.processPopularProducts(doc);
} else if ("PROMOTED_SUGGESTION" == doc.doctype &&
(this.options.promotedSuggestions.count > this.currentResults.PROMOTED_SUGGESTION.length) &&
this.isUnique(doc.autosuggest, uniqueSuggestions)) {
this.processPromotedSuggestion(doc);
}
}

Expand Down Expand Up @@ -1769,6 +1853,16 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
+ '{{/each}}'
+ '{{/if}}';
}
, preparepromotedSuggestionsHTML: function () {
return '{{#if data.PROMOTED_SUGGESTION}}' +
(this.options.promotedSuggestions.header ? '<li class="unbxd-as-header">' + this.options.promotedSuggestions.header + '</li>' : '') +
'{{#each data.PROMOTED_SUGGESTION}}' +
'<li class="unbxd-as-keysuggestion" data-value="{{autosuggest}}" data-index="{{@index}}" data-type="{{type}}" data-source="{{source}}">' +
(this.options.promotedSuggestions.tpl ? this.options.promotedSuggestions.tpl : this.default_options.promotedSuggestions.tpl) +
'</li>' +
'{{/each}}' +
'{{/if}}';
}
, preparetopQueriesHTML: function () {
return '{{#if data.TOP_SEARCH_QUERIES}}'
+ (this.options.topQueries.header ? '<li class="unbxd-as-header">' + this.options.topQueries.header + '</li>' : '')
Expand Down Expand Up @@ -1807,6 +1901,8 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
}
else if (key === "topQueries") {
key = "TOP_SEARCH_QUERIES";
} else if (key === "promotedSuggestions") {
key = "PROMOTED_SUGGESTION";
}
else
key = "KEYWORD_SUGGESTION";
Expand Down Expand Up @@ -1843,7 +1939,7 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
}

if (!self.currentResults['IN_FIELD'].length && !self.currentResults['KEYWORD_SUGGESTION'].length
&& !self.currentResults['POPULAR_PRODUCTS'].length && !self.currentResults['TOP_SEARCH_QUERIES'].length && this.options.noResultTpl) {
&& !self.currentResults['POPULAR_PRODUCTS'].length && !self.currentResults['TOP_SEARCH_QUERIES'].length && !self.currentResults['PROMOTED_SUGGESTION'].length && this.options.noResultTpl) {

if (typeof this.options.noResultTpl === "function") {
html = html + '<li>' + this.options.noResultTpl.call(self, encodeURIComponent(self.params.q)) + '</li>';
Expand All @@ -1870,6 +1966,9 @@ var unbxdAutoSuggestFunction = function ($, Handlebars, params) {
else if (key === "topQueries") {
key = "TOP_SEARCH_QUERIES";
}
else if (key === "promotedSuggestions") {
key = "PROMOTED_SUGGESTION"
}
else
key = "KEYWORD_SUGGESTION";
sidelen = sidelen + self.currentResults[key].length;
Expand Down

0 comments on commit ec58e77

Please sign in to comment.