如何限制广告客户在特定日期在Javascript中展示广告的次数?

时间:2015-11-29 14:59:49

标签: javascript ajax

以下是该方案:

在我们的网站上,我们有3位广告客户。广告客户让我们收集他们产品的电子邮件。

每位广告客户都有每天收集的最大电子邮件数量上限,例如500.一旦我们在某一天收到500封电子邮件,我们就需要停止展示广告客户提供的内容。

要构建广告商堆栈,我们有一个Javascript数组,用于确定要为给定用户显示的广告商。我们将每个广告客户推送到数组中。如果我们没有广告客户,我们会将用户重定向到另一个页面。

基本上我需要做的是构建一个最高可达500的迭代,如果我们在某一天收到500封电子邮件,我们就不会显示该广告客户。

// 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; ?>";
        }

    });

1 个答案:

答案 0 :(得分:0)

要计算数字并显示广告,您需要在服务器和客户端之间拆分工作。 这里的关键是服务器在客户端请求每日视图时维护每日视图并要求更新它们。

首先,您需要为客户端和服务器设置两个端点,以通过http / https:

进行通信
  1. HttpPost www.example.com/advertiser-1/daily-views [告诉你 已查看 advertiser-1 的服务器,因此每天都会增加 意见]

  2. HttpGet www.example.com/advertiser-1/daily-views [获取 来自服务器的 advertiser_1 的每日观看次数

  3. 在客户端,

    1. 您需要向服务器发出ajax调用(获取请求)以获取每日计数。

    2. 查看广告时,您需要向服务器发出ajax调用(发布请求),以更新广告客户的每日观看次数。

    3. 在服务器端,

      1. 对于get请求,返回包含每日视图数的json对象
      2. 对于发布请求,增加每日观看次数并返回&#34; ok&#34; (或任何你想要的)