Join Free
+ Reply to Thread
Results 1 to 6 of 6
  1. #1

    Howto: Display top products from trends for specific query

    The following code shows you how to query the trends api endpoint with a specific query and date range. What you get back is a list of facets. We use the productID facet to pull back products from the product endpoint so we can display them on our site. Rather than pass over the products one at a time and loop through them, we pass them over as an array so everything can be done in a single query. Specificially, we pass them over to filterProductId which, as the name implies, allows us to get back the products associated with a specific list of productId's.

    PHP Code:
    <?php 
    require_once('Prosperent_Api.php'); 
    $prosperentApi = new Prosperent_Api(array( 
            
    'filterKeyword'      => shoes,
            
    'commissionDateRange' => '20120101,20120701',
            
    'enableFacets' => 'productId'
        
    )); 
        
    //fetch the result 
        
    $prosperentApi->fetchTrends(); 

    //loop through productId facet and place productId's in an array we can access later
    foreach ($prosperentApi->getFacets('productId') as $productId
       { 
       
     
    $productArray[] = $productId['value'];
       
       }

    //query product endpoint and pass over productId array to productId filter    
    require_once('Prosperent_Api_new.php');
        
    $prosperentApi = new Prosperent_Api(array(
          
    'api_key'          => insertApiKeyHere,
          
    'filterProductId' => $productArray,
          
    'visitor_ip'        => $_SERVER['REMOTE_ADDR'],
          
    'channel_id'      => 0,
          
    'page'              => 1,
          
    'imageSize'       => '125x125',
          
    'limit'               => 10  
      
    )); 
    $results $prosperentApi->fetch();
    foreach (
    $prosperentApi->getData() as $record
        { 
    ?>
        <li id="serp2" class="result">
          <div class="result-body">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="125px"><div style="width: 125px; height: 125px; overflow: hidden;"><a class="UrlTitleLine ui-widget-content "  
    href="<?=$record['affiliate_url']?>" dest="<?=$record['affiliate_url']?>" orig="<?=$record['affiliate_url']?>" pos="1"
    title="<?=$record['keyword']?>" ><img
     src="<?=$record['image_url']?>"  alt="<?=$record['keyword']?>" title="<?=$record['keyword']?>" width=125 height=125></a></div></td>
              </tr>
            </table>
            <div class="url-line"> <span class="resultUrl ">
              <?=$record['merchant']?>
              </span> <span class="non-url"> &nbsp;&nbsp;&ndash;&nbsp; $
              <?=$record['price']?>
              &nbsp;
              <?=$record['brand']?>
              </span>
              <div id="tools1" class="pop-shadow-small round5 tools-pop"></div>
            </div>
          </div>
        </li>
        <?
    }  
    ?>

  2. #2
    I found a bug with my above code.

    When using the "query" parameter, your search is performed against all searchable fields in the trends endpoint. Normally this is no big deal, but in a few very rare instances it can cause a problem. Take the query ipad for example. Since one of the fields in trends is deviceModel, this means that instead of pulling back products related to ipad's, you are actually pulling back items that were sold to someone browsing prosperent sites ON an ipad. Luckily the fix is simple, use filterKeyword instead:

    replace:

    'query' => shoes,

    with:

    'filterKeyword' => shoes,

  3. #3
    Updated to make use of the new feature in the api to enable/disable facets. In this case, we only need the productId in order to pull back the trends data. By adding 'enableFacets' => 'productId' we save 16 queries, and loads of bandwidth on each call.

  4. #4
    Is there a Nodejs Library of proseperent ?

  5. #5
    Not that I have come across. Someone here may have one that I am unaware of though.

  6. #6
    I wanted to post an alternative to the above code. The above grabs the productId from the trends api, and searches the product catalog for that exact productId. This is great when you want an exact list of top sellers, but sometimes you don't need something so exact. In these cases, you can instead use the keyword facet, then perform a search against the product catalog for a keyword instead of a productId. This returns far more results although some of them may be approximate matches. This is also valuable when looking at older data. Remember that our productId's changed a few months ago when we switched over to using merchant Id's instead of merchant names in our system. Here is the code:

    PHP Code:
    <?php 
    require_once('Prosperent_Api.php'); 
    $prosperentApi = new Prosperent_Api(array( 
            
    'filterKeyword'      => '*shoe*',
            
    'commissionDateRange' => '20120101,20120701',
            
    'enableFacets' => 'keyword'
        
    )); 
        
    //fetch the result 
        
    $prosperentApi->fetchTrends(); 

    //loop through keyword facet and place keywords in an array we can access later
    foreach ($prosperentApi->getFacets('keyword') as $keyword
       { 
       
     
    $productArray[] = $keyword['value'];
       
       }

    //query product endpoint and pass over productId array to productId filter    
    require_once('Prosperent_Api_new.php');
        
    $prosperentApi = new Prosperent_Api(array(
          
    'api_key'          => insertApiKeyHere,
          
    'filterKeyword' => $productArray,
          
    'visitor_ip'        => $_SERVER['REMOTE_ADDR'],
          
    'channel_id'      => 0,
          
    'page'              => 1,
          
    'imageSize'       => '125x125',
          
    'limit'               => 100  
      
    )); 
    $results $prosperentApi->fetch();
    foreach (
    $prosperentApi->getData() as $record
        { 
    ?>
        <li id="serp2" class="result">
          <div class="result-body">
            <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="125px"><div style="width: 125px; height: 125px; overflow: hidden;"><a class="UrlTitleLine ui-widget-content "  
    href="<?=$record['affiliate_url']?>" dest="<?=$record['affiliate_url']?>" orig="<?=$record['affiliate_url']?>" pos="1"
    title="<?=$record['keyword']?>" ><img
     src="<?=$record['image_url']?>"  alt="<?=$record['keyword']?>" title="<?=$record['keyword']?>" width=125 height=125></a></div></td>
              </tr>
            </table>
            <div class="url-line"> <span class="resultUrl ">
              <?=$record['merchant']?>
              </span> <span class="non-url"> &nbsp;&nbsp;&ndash;&nbsp; $
              <?=$record['price']?>
              &nbsp;
              <?=$record['brand']?>
              </span>
              <div id="tools1" class="pop-shadow-small round5 tools-pop"></div>
            </div>
          </div>
        </li>
        <?
    }  
    ?>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts