We're adding advertising to our Craft site. The ads come from our business members and are displayed randomly with article pages, based on tags and categories. All that is working fine.

The question comes up, how to report impression counts of the ads back to the advertisers? I'm newish to Craft; I don't know if it is possible to write back to a field of an asset (displayCount = displayCount + 1 kind of thing).

Any ideas or suggestions would be appreciated.

  • Have you considered using a plugin like Ad Wizard? (Disclaimer: I am the author)
    – Lindsey D
    Dec 28 '18 at 20:32
  • No, have not. But I will. Thanks.
    – Mosswalker
    Dec 29 '18 at 2:40

Since it sounds like you're rolling-your-own, and can afford a relatively simple implementation, I'd recommend checking out the seq(…) Twig function that Craft provides.

{% do seq("ad-impressions-#{ad.uid}") %}

Then, if you have a dashboard of some kind for advertisers/partners, you could display those tallies by iterating over their “owned” advertisements:

{% for ad in ads %}
    {{ ad.title }} views: {{ seq("ad-impressions-#{ad.uid}", null, false) }}
{% endfor %}

Here, the third argument tells the function to read the value, but not increment it.

Of course, this is extremely crude. It assumes you only need to count the number of times an ad is rendered into a page, and makes no guarantees about how many times it’s actually seen. It also won't increment if the page is cached.

Overall, I'd recommend outputting the ad with some kind of identifying information, and firing a custom Google Analytics event… or—time permitting—write a custom controller to ping via AJAX when an ad is actually displayed in a browser. Keep in mind that somewhere in here, you're apt to be able to deliver a better product with an existing plugin! 😉

  • Thank you. This is just the kind of information I was looking for.
    – Mosswalker
    Dec 30 '18 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.