问题描述:

Here's the scenario:

On our site, we have 3 advertisers. The advertisers have us collecting emails for their product.

Each advertiser has a cap for the max amount of emails collected per day, say 500. Once we reach 500 emails in a given day, we need to stop showing that advertisers offer.

To build the advertiser stack, we have a Javascript array that determines which advertisers to be shown for a given user. We push each advertiser into the array. If we have no advertisers, we redirect the user to another page.

Basically what I need to do is build an iteration that counts up to '500', and if we get 500 emails in a given day, we don't show that advertiser.

// what offers will be show

var offer_order = [];

// what needs to be written is an iteration that counts up to '500',

// then if its in a given day, do not show the respective offer

// add the advertisers to the order being shown

// if ( cap <= '500' ) {

offer_order.push('advertiser-1');

// }

// if ( cap <= '300' ) {

offer_order.push('advertiser-2');

// }

// this offer has no cap, so it will always be shown

offer_order.push('advertiser-3');

// I removed all of the functionality that sets the starting offer

// and controls showing the next offer, so don't worry about this not working

// send lead data on submit

$('.submit').on('click', function() {

$.ajax({

url: 'http://domain.com/send-data.php',

data: $('#user_data').serialize(),

type: 'POST',

}).done(function(responseData) {

// in our response data, we either get back 'success' or 'fail'

// we need to build our iteration here

// if the beginning offer is equal to whats next, then were done, so lets move to the next page

if ( next == $(starting_offer).data("start") ) {

window.location.href = "<?php echo $next_page; ?>";

}

}).fail(function() {

// if the beginning offer is equal to whats next, then were done, so lets move to the next page

if ( next == $(starting_offer).data("start") ) {

window.location.href = "<?php echo $next_page; ?>";

}

});

网友答案:

To count the number and ad is displayed, you need to split the work between server and client. The key here is server is maintaining the daily-views while client request for daily-views and ask to update them.

First you need to set up two endpoints for client and server to communicate via http/https:

  1. HttpPost www.example.com/advertiser-1/daily-views [to tell the server that advertiser-1 has been viewed and so increase its daily views ]

  2. HttpGet www.example.com/advertiser-1/daily-views [to get daily views of advertiser_1 from server]

On client side,

  1. You need to make ajax call(Get request) to server to get the daily count.

  2. When an advertisement is viewed, you need to make an ajax call(Post request) to server to update the advertisers daily views.

On server side,

  1. For get request, return json object containing number of daily views
  2. For post request, increment the count of daily views and return "ok" (or whatever you want)
相关阅读:
Top