# How to implement a download counter

**URL:** <https://forum.getkirby.com/t/how-to-implement-a-download-counter/12765>\
**Category:** Questions\
**Tags:** v2\
**Created:** [February 8, 2019, 11:28am UTC](https://forum.getkirby.com/t/how-to-implement-a-download-counter/12765 "2019-02-08T11:28:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![daybugging](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@daybugging](https://forum.getkirby.com/u/daybugging)\
**Post date:** [February 8, 2019, 11:28am UTC](https://forum.getkirby.com/t/how-to-implement-a-download-counter/12765/1 "2019-02-08T11:28:30Z")

</div>

I want to implement a download counter, that’s my page structure:

```auto
parent
  - child1 > file.zip
  - child2 > file.zip

```

On the parent page, all its children are displayed, with text, images etc and more importantly, they all contain a link to `file.zip`, which is attached to each child page:

```php
<?php foreach($page->children() as $article) : ?>
<a class="download" href="<?= $article->zip()->url() ?>">Download me</a>
<?php endforeach ?>

```

Now, inside the children’s template, let’s call it `article.php`, there’s the AJAX php code mentioned above, but with [`$page->update()`](https://getkirby.com/docs/reference/objects/page/update):

```php
if(r::ajax()) {
  $page->update([
    'clicks' => function($clicks) {
      return $clicks++;
    }
  ], null, $kirby->impersonate('kirby'));
} 

```

… and since clicking is client-side and PHP is server-side, we’d have to make an AJAX call from, say, `parent.js`:

```js
// jQuery version
$(function(){
  $('.download').on('click', function(e) {
    var url = $(this).attr('href');
    $.get(url);
  });
});

```

… whereas a non-jQuery version probably would look something [like this](http://youmightnotneedjquery.com/#request).

Right?

---

<div class="post-metadata">

**Author:** ![sylvainjule](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/sylvainjule/32/4338_2.png) [@sylvainjule](https://forum.getkirby.com/u/sylvainjule)\
**Post date:** [February 8, 2019, 12:53pm UTC](https://forum.getkirby.com/t/how-to-implement-a-download-counter/12765/2 "2019-02-08T12:53:16Z")

</div>

First, you need to actually access the zip file in your loop to be able to call its `url()` method (with `toFile`).  
Remember that the `href` attribute now contains a link to the file itself, not its parent page, so you can’t expect to use that as url param in your ajax call. You need to add another reference to the url of the page itself, for example in a `data-parent` attribute:

```php
<?php foreach($page->children() as $article) : ?>
    <?php if($zip = $article->zip()->toFile()): ?>
        <a class="download" href="<?= $zip->url() ?>" data-parent="<?= $article->url() ?>">Download me</a>
    <?php endif; ?>
<?php endforeach ?>

```

Then your jquery call. The appropriate method would be `post` (get requests data, post submits data), and you could instead of listening to every ajax call, explicitely state the action you want to make :

```javascript
$('.download').on('click', function(e) {
    var url = $(this).attr('data-parent');
    $.post(url, { increment: true });
});

```

Later in your controller, you check if there is an `increment` param given, and there’s actually a handy helper for what you want to achieve:

```php
if(get('increment')) {
    $page->increment('clicks');
} 

```

---

<div class="post-metadata">

**Author:** ![bvdputte](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/bvdputte/32/8179_2.png) [@bvdputte](https://forum.getkirby.com/u/bvdputte)\
**Post date:** [February 8, 2019, 1:58pm UTC](https://forum.getkirby.com/t/how-to-implement-a-download-counter/12765/3 "2019-02-08T13:58:43Z")

</div>

Not completely knowing your use case, but I wouldn’t save this kind of info as “content” in my Kirby setup.

I’ld rather use Google Analytics or Matomo for this kind of purpose. Usually those kind of “analytics” softwares have api’s for these kinds of purposes, and this can be visualised in their dashboards then.

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:23pm UTC](https://forum.getkirby.com/t/how-to-implement-a-download-counter/12765/4 "2025-02-06T17:23:20Z")

</div>

Archived on February 6, 2025, 5:23pm UTC


