Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

Profile Script to return Recent searches

philip_k
Level 2
Level 2

I was tasked to create an activity where we return the user's last 5 search terms when the user clicks on the  search bar. To do this I've created a profile script that takes the user's search terms passed through a parameter in the global mbox. The script then stores these terms in the visitor profile and it works for a day or so until the SYSTEM deactivates it because it executes more than 2000 instructions. Is there a way to keep this script from executing too many instructions? How can I limit the number of search terms returned from the mbox to 5?

Here is the latest version after countless attempts.

PROFILE SCRIPT NAME: SearchTerms_Live

if (mbox.name == 'global') {

if (mbox.param('PageType') == 'keywordsearch') {

  function uniq(a) {

          var prims = {"boolean":{}, "number":{}, "string":{}}, objs = [];

          return a.filter(function(item) {

              var type = typeof item;

              if(type in prims)

                  return prims[type].hasOwnProperty(item) ? false : (prims[type][item] = true);

              else

                  return objs.indexOf(item) >= 0 ? false : objs.push(item);

          });

     }

        

     var term =  mbox.param('searchterm');

     var  prevTerms = user.get('SearchTerms_Live');

      if (prevTerms !== null) {           

       var searchTerms = prevTerms;

       var newSearchArray = [];

       newSearchArray = searchTerms.split(",");   

       newSearchArray.splice(0, 0, term);

       var uniqueTermsArray = uniq(newSearchArray);

       var recentTermsArray = uniqueTermsArray.splice(0, 5);

       var newTermsString = recentTermsArray.toString();

     } else {

       var newTermsString = term;

     }

   return newTermsString ;

  }

}

0 Replies