我如何限制广告商的次数'的广告在某一天用Javascript显示

How can I cap the number of times an advertiser's ad is shown in a given day in Javascript?

本文关键字:某一天 Javascript 显示 何限制 广告商      更新时间:2023-09-26

以下是场景:

在我们的网站上,我们有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; ?>";
        }
    });

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

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

  1. HttpPost www.example.com/advertiser-1/daily-views[告诉advertiser-1已被查看的服务器,因此每天都在增加视图]

  2. HttpGet www.example.com/advertiser-1/daily-views[获取advertiser_1从服务器的每日浏览量]

在客户端,

  1. 您需要对服务器进行ajax调用(Get请求)来获取每日计数。

  2. 查看广告时,您需要向服务器发出ajax调用(Post请求),以更新广告商的每日视图。

在服务器端,

  1. 对于get请求,返回包含每日视图数的json对象
  2. 对于post请求,增加每日浏览次数并返回"ok"(或任何您想要的)