
{% for ad in ads %}
<div class="ad-container">
  <a href="{{ ad.target_url }}" class="ad-item" data-ad-id="{{ ad.id }}">
    {{ ad.content|safe }}
  </a>
</div>
{% endfor %}

<script>
$(document).ready(function() {
  // 展现量统计
  $.ajax({
    url: '/api/record_impression/',
    method: 'POST',
    data: {
      'ad_ids': $('.ad-item').map(function() {
        return $(this).data('ad-id');
      }).get(),
      'csrfmiddlewaretoken': '{{ csrf_token }}'
    }
  });

  // 点击量统计
  $('.ad-item').click(function(e) {
    e.preventDefault();
    var adId = $(this).data('ad-id');
    var targetUrl = $(this).attr('href');
    
    $.ajax({
      url: '/api/record_click/',
      method: 'POST',
      data: {
        'ad_id': adId,
        'csrfmiddlewaretoken': '{{ csrf_token }}'
      },
      success: function() {
        window.location.href = targetUrl;
      }
    });
  });
});
</script>
