1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PDF download counter in Xenforo

Discussion in 'XenForo Questions and Support' started by Andy.N, Jun 21, 2012.

  1. Andy.N

    Andy.N Well-Known Member

    I have a PDF guide that I'd like to make available for public download. I can put this PDF on my server or on a CDN. I'd like to have a counter on the sidebar of my site to display how many times this PDF has been downloaded.

    Anyone has any idea how to do this with using Widget Framework?
    Here is some info I find online
  2. Sadik B

    Sadik B Well-Known Member

    Andy.N likes this.
  3. Andy.N

    Andy.N Well-Known Member

    I hope some developers will take on this. I'll try to play around with this.

    On another note, if we have Resource manager, we can show the download count for this!!!
  4. Andy.N

    Andy.N Well-Known Member

  5. Andy.N

    Andy.N Well-Known Member

    I'm thinking of other ways to accomplish this.

    I will make the PDF an attachment to a post and let guests download them. I will then use the download count for this attachment and display it on sidebar or inside User Notice.

    Is there a way to download this info directly?
  6. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    Create a listener for the template_hook event:

    Admin CP -> Development -> Code Event Listeners

    Here is some example code:

    class Class_Name
    	public static function pdfCount($hookName, &$contents, array $hookParams, XenForo_Template_Abstract $template)
    		if ($hookName == 'forum_list_sidebar')
    			$db = XenForo_Application::get('db');
    			$count = $db->fetchOne("
    				SELECT view_count
    				FROM xf_attachment
    				WHERE attachment_id = 1
    			$contents .= "PDF viewed {$count} times.";
    You can add your own includes in there if you need to use a layout file.
    Andy.N likes this.
  7. Kintaro

    Kintaro Well-Known Member

    Andy, please, when you get it to work release it on the RM.
    In this ways info get not lost.
  8. Andy.N

    Andy.N Well-Known Member

    Sounds very good, Jake.
    1) Is there a list of hook names that I can use? I see that you use "forum_list_sidebar". What if I want to add it in the Notices?
    2) Can you provide some example layout file and how I can use it?
    Thanks very much.
  9. Jake Bunce

    Jake Bunce XenForo Moderator Staff Member

    1) Hook names are inline with the template code. Look in the template where you want to add content and you will usually see xen:hook tags.

    2) You probably want to render a template and then append the output of that template to the contents of the hook. Code example:


    For layout code you can look to the other sidebar blocks such as those in:

    Admin CP -> Appearance -> Templates -> forum_list
  10. Andy.N

    Andy.N Well-Known Member

  11. Andy.N

    Andy.N Well-Known Member

  12. xfrocks

    xfrocks Well-Known Member

    I haven't read through the link but I'm fairly certain that doing so won't work for you because the counter will showup in Google Analytics dashboard but not in place. Basically you will need a custom add-on or using some external tracker service.
    Jake Bunce likes this.
  13. Andy.N

    Andy.N Well-Known Member

    Yes, I now only need to track this on Analytics and don't need XF to keep track of this. It appears the url bbcode does not allow any optional beside the http.
  14. xfrocks

    xfrocks Well-Known Member

    You can use the Custom BB Code Manager then ;)
    Andy.N likes this.
  15. Andy.N

    Andy.N Well-Known Member

    Ah ha. Why didn't I think of it? ;)
  16. Digital Doctor

    Digital Doctor Well-Known Member

  17. Andy.N

    Andy.N Well-Known Member

Share This Page