PriMoThemes — now s2Member® (official notice)

This is now a very OLD forum system. It's in READ-ONLY mode.
All community interaction now occurs at WP Sharks™. See: new forums @ WP Sharks™

counting downloads

s2Member Plugin. A Membership plugin for WordPress®.

counting downloads

Postby Harty » July 11th, 2011, 6:53 pm

Hi.
Members of my site have access to pages where they can download PDFs for free.
Is there any way to see how many of each one are downloaded?

Thanks.
User avatar
Harty
Registered User
Registered User
 
Posts: 104
Joined: April 27, 2011
Location: New Zealand

Re: counting downloads

Postby Cristián Lávaque » July 11th, 2011, 10:04 pm

Not with s2Member. You may want to use your server stats, or set up Google Analytics to track the links to those files. Maybe there's another plugin that'd do this over at WordPress Extend. Try searching Google to find help to count file downloads.
Cristián Lávaque http://s2member.net
Is s2Member working for you? Please rate it Image at WordPress.org. Thanks! :)
User avatar
Cristián Lávaque
Developer
Developer
 
Posts: 6836
Joined: December 22, 2010

Re: counting downloads

Postby colinp386 » September 29th, 2011, 8:03 pm

I am looking for something similar our site is all about downloading software files.
I am using S2member to authenticate and control subscription billing.
I have just started experimenting with the download levels to control the number of downloads a member can access a month, and yes it's very slick! Kudos! :D

However; I would like to be able to:
1) Show the total number of downloads a user has made in their user profile.
2) an ability to send our admin a warning message when a user has reached and/or exceeded their download quota.

This way we can run a report and either cut folks off manually, or push them to upgrade to a higher download subscription.

The most important is the ability to be able to count the number of download in a given period and/or overall.
You must be doing this anyway as you give the number in the popup when they select the download ;) I was a little surprised not to find it in the user display or did I miss it LoL

Many thanks!

colin
Colin Prior


UnixPackages - Open Source Packages for Solaris
User avatar
colinp386
Registered User
Registered User
 
Posts: 28
Joined: September 29, 2011

Re: counting downloads

Postby Jason Caldwell » October 3rd, 2011, 10:33 pm

Yes, s2Member keeps a log for each Member.

Code: Select all
<?php
$user_id 
= 1;
$downloads = get_user_option("s2member_file_download_access_log", $user_id);
print_r($downloads); /* Array of file downloads in the current period of time, as configured by the site owner under Basic Download Restrictions for s2Member. */
?>
The array contains one element for each unique file downloaded, and it includes the date the file was downloaded, and the name of the file. This record keeping has existed in s2Member since v1, but I've never gotten around to building a UI panel for this data.

This additional data is also available.
Code: Select all
<?php
$user_id 
= 1;
$archived_downloads = get_user_option("s2member_file_download_access_arc", $user_id);
print_r($archived_downloads); /* Array of file downloads for previous periods of time, as configured by the site owner under Basic Download Restrictions for s2Member. */
?>


All of that being said, if you just need basic information, s2Member provides that through API Constants, which are MUCH easier to work with. See: viewtopic.php?f=40&t=12449&src_doc_v=111003#src_doc_S2MEMBER_CURRENT_USER_DOWNLOADS_CURRENTLY
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: counting downloads

Postby colinp386 » October 3rd, 2011, 11:17 pm

Many thanks!!

Very helpful, I will go and play tomorrow!
I did find the API constants in the end, and that got me part the way there :-)

Kudos for the great plugin, once we start to make revenue we will be upgrading and sending you some $$ :-)
Colin Prior


UnixPackages - Open Source Packages for Solaris
User avatar
colinp386
Registered User
Registered User
 
Posts: 28
Joined: September 29, 2011

Re: counting downloads

Postby guardian » October 7th, 2011, 8:15 am

Hi!

I know you are working very hard on improving the plugin. Some features are:
    mandatory
    obligatory
    optional
    de luxe ones

A nice stat feature would be probably in the optional category or in the "de luxe" one

Anyway, I am suggesting that a stat feature be added somewhere on the line. I know there is an API but let's say I am on a dummy side for all php "stuff".

Maybe in 2012!!! :geek:


Roger
User avatar
guardian
Registered User
Registered User
 
Posts: 17
Joined: May 24, 2011

Re: counting downloads

Postby Jason Caldwell » October 7th, 2011, 7:20 pm

Thank you both, and thanks for the great feedback. Much appreciated.
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: counting downloads

Postby lmllewellyn » October 15th, 2011, 10:30 am

Is there an easy way to see the total number of times a file has been downloaded by unique members (not per member, but overall)?
User avatar
lmllewellyn
Registered User
Registered User
 
Posts: 17
Joined: August 31, 2011

Re: counting downloads

Postby kennymcnett » October 21st, 2011, 11:25 am

Any update to counting the number of a specific file's downloads, per user?

It seems that s2 only tracks unique downloads, but I need to count total downloads of each file, per user.
User avatar
kennymcnett
Registered User
Registered User
 
Posts: 23
Joined: October 5, 2011
Location: Los Angeles

Re: counting downloads

Postby colinp386 » October 26th, 2011, 3:39 pm

Jason,

Thanks again for the info on the function.
popped it into a shortcode that I placed into themes functions.php
Code: Select all
/*
Shortcode Name: pkg-downloads
Description: if activated, will show the downloaded files for current user.
Author: Colin
Version: 1.0
*/

add_shortcode('pkg-downloads', 'pkg_downloads_shortcode');
function pkg_downloads_shortcode() {
   global $user_id;
   $current_user = wp_get_current_user();
   if ( !($current_user instanceof WP_User) )
     return;
   echo 'User ID: ' . $current_user->ID . '<br />';
   $user_id = $current_user->ID;
   $downloads = get_user_option("s2member_file_download_access_log", $user_id);
   echo 'Current Downloads<br />';
   print_r($downloads);
   echo '<br />';
   echo 'Archived Downloads<br />';
   $archived_downloads = get_user_option("s2member_file_download_access_arc", $user_id);
   print_r($archived_downloads);
}


And it works fine, however as you note, you get all whole array of data.
Any tips on parsing out just the Date and File?

Cheers

Colin
Colin Prior


UnixPackages - Open Source Packages for Solaris
User avatar
colinp386
Registered User
Registered User
 
Posts: 28
Joined: September 29, 2011

Re: counting downloads

Postby Jason Caldwell » October 26th, 2011, 4:17 pm

Something like this maybe.
Code: Select all
<?php
$archived_downloads 
= get_user_option ("s2member_file_download_access_arc", $user_id);
if (is_array ($archived_downloads) && !empty ($archived_downloads))
    {
        foreach ($archived_downloads as $archived_download)
            {
                $date = $archived_download["date"];
                $file = $archived_download["file"];
                // Do something custom here if you like.
            }
    }
?>
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: counting downloads

Postby lmllewellyn » October 26th, 2011, 4:31 pm

As I asked earlier, is there a way to count the total number of downloads (but unique users) per file?
User avatar
lmllewellyn
Registered User
Registered User
 
Posts: 17
Joined: August 31, 2011

Re: counting downloads

Postby Jason Caldwell » October 26th, 2011, 5:32 pm

lmllewellyn wrote:As I asked earlier, is there a way to count the total number of downloads (but unique users) per file?
s2Member currently does NOT store information in a site-wide manner for file downloads. That is, s2Member counts each unique file download exactly ONE time per User/Member, and that's how it's stored by s2Member ( i.e. on a per-User basis ), and not collectively.

That being said, you could do something like this, which would give you the total number of Users/Members that have downloaded a particular file. Remember though, if a particular User/Member downloads the same exact file 10 times, s2Member only counts that as ONE file download
( this is the intended behavior ).

Create this directory and file:
/wp-content/mu-plugins/s2-hacks.php ( these are MUST USE plugins, that's what you want ).
Reference article: http://codex.wordpress.org/Must_Use_Plugins
Code: Select all
<?php
function s2_total_user_downloads_of 
($file, $check_archives_too = TRUE)
    {
        global $wpdb; /* Global database object reference. */
        /**/
        $q1 = mysql_query ("SELECT SQL_CALC_FOUND_ROWS DISTINCT `user_id` FROM `" . $wpdb->usermeta . "` WHERE (`meta_key` = '" . $wpdb->prefix . "s2member_file_download_access_log'" . (($check_archives_too) ? " OR `meta_key` = '" . $wpdb->prefix . "s2member_file_download_access_arc'" : "") . ") AND `meta_value` REGEXP '.*\"file\";s:[0-9]+:\"" . esc_sql ($file) . "\".*' LIMIT 1", $wpdb->dbh);
        $q2 = mysql_query ("SELECT FOUND_ROWS()", $wpdb->dbh);
        /**/
        $users = (int)mysql_result ($q2, 0);
        /**/
        mysql_free_result($q2);
        mysql_free_result($q1);
        /**/
        return $users;
    }
?>
Now, with this custom function in place, you could do something like this:
Code: Select all
<?php echo s2_total_user_downloads_of("example-file.zip"); ?>
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: counting downloads

Postby kennymcnett » October 26th, 2011, 5:36 pm

Remember though, if a particular User/Member downloads the same exact file 10 times, s2Member only counts that as ONE file download ( this is the intended behavior ).


Any way to skirt this? I need to show how many times a user has downloaded a file. I don't need any restrictions, just the number.
User avatar
kennymcnett
Registered User
Registered User
 
Posts: 23
Joined: October 5, 2011
Location: Los Angeles

Re: counting downloads

Postby Jason Caldwell » October 26th, 2011, 6:13 pm

kennymcnett wrote:
Remember though, if a particular User/Member downloads the same exact file 10 times, s2Member only counts that as ONE file download ( this is the intended behavior ).


Any way to skirt this? I need to show how many times a user has downloaded a file. I don't need any restrictions, just the number.
No, not really. But, you CAN handle this yourself using an Action Hook for s2Member's file download processing routine. In other words, you can keep your own count if you like, and your own count can tally the total number of times a file is downloaded, period. That is, this would NOT be limited to counting each file only ONE time. I've provided a code sample below to accomplish this.

Create this directory and file:
/wp-content/mu-plugins/s2-hacks.php ( these are MUST USE plugins, that's what you want ).
Reference article: http://codex.wordpress.org/Must_Use_Plugins
Code: Select all
<?php
add_action 
("ws_plugin__s2member_during_file_download_access", "my_s2_file_download_counts");
function my_s2_file_download_counts ($vars = array ())
    {
        if (($serving = $vars["serving"]) && ($file = $vars["req"]["file_download"]) && ($user_id = $vars["user_id"]))
            {
                $counts = (array)get_user_option ("my_s2_file_download_counts", $user_id);
                $counts[$file] = (!empty ($counts[$file])) ? (int)$counts[$file] + 1 : (int)1;
                update_user_option ($user_id, "my_s2_file_download_counts", $counts);
            }
    }
function my_total_user_downloads_of ($file, $user = FALSE)
    {
        if (($user = (is_object ($user)) ? $user : ((is_user_logged_in ()) ? wp_get_current_user () : false)) && !empty ($user->ID) && ($user_id = $user->ID))
            {
                $counts = (array)get_user_option ("my_s2_file_download_counts", $user_id);
                $total = (!empty ($counts[$file])) ? (int)$counts[$file] : (int)0;
            }
        return (!empty ($total)) ? $total : 0;
    }
?>
* Requires s2Member v111017+ for this to work as expected.

Now, with this file in place, you can do something like this:
Code: Select all
<?php echo my_total_user_downloads_of("example-file.zip"); // For the current User. ?>
Or, for a specific User, instead of for the currently logged-in User:
Code: Select all
<?php
$user 
= new WP_User("123"); /* For user ID #123 ( i.e. a specific User ). */
echo my_total_user_downloads_of("example-file.zip", $user); /* For a specific User. */
?>
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: counting downloads

Postby kennymcnett » October 26th, 2011, 6:22 pm

This is awesome. Thank you so much! I really love your technical support.
User avatar
kennymcnett
Registered User
Registered User
 
Posts: 23
Joined: October 5, 2011
Location: Los Angeles

Re: counting downloads

Postby colinp386 » October 27th, 2011, 12:14 pm

Jason Caldwell wrote:Something like this maybe.
Code: Select all
<?php
$archived_downloads 
= get_user_option ("s2member_file_download_access_arc", $user_id);
if (is_array ($archived_downloads) && !empty ($archived_downloads))
    {
        foreach ($archived_downloads as $archived_download)
            {
                $date = $archived_download["date"];
                $file = $archived_download["file"];
                // Do something custom here if you like.
            }
    }
?>


Thanks Jason, that helped a lot, I now have a short-code that displays current and past files and the total downloads in the header.
Colin Prior


UnixPackages - Open Source Packages for Solaris
User avatar
colinp386
Registered User
Registered User
 
Posts: 28
Joined: September 29, 2011

Re: counting downloads

Postby cassel » October 27th, 2011, 12:29 pm

Although i am not very techie, i use StatCounter on my site to track overall download of each file. It might be of interest to some. Who knows?
User avatar
cassel
Experienced User
Experienced User
 
Posts: 442
Joined: February 17, 2011

Re: counting downloads

Postby kennymcnett » December 1st, 2011, 12:32 pm

Jason, does this code work for counting downloads made with an Advanced Download Key? It doesn't seem to, from my end.

If not, is there any way to do that? Inside my posts, I use the short code:
Code: Select all
[s2File download="example.zip" download_key="true" /]


I need to display next to that link the number of times the current user has downloaded that file. Or, at the very least, whether the current user has downloaded the file previously (no count, just a boolean toggle).

Ideas?

Thank you!
User avatar
kennymcnett
Registered User
Registered User
 
Posts: 23
Joined: October 5, 2011
Location: Los Angeles

Re: counting downloads

Postby Jason Caldwell » December 2nd, 2011, 3:46 am

Thanks for the excellent question.

With a Download Key, not yet.

The latest release of s2Member v111105 did introduce some new API Functions for counting downloads though, so you might want to have a look at each these documents.

viewtopic.php?f=40&t=12453&src_doc_v=111105#src_doc_s2member_total_downloads_of%28%29
viewtopic.php?f=40&t=12453&src_doc_v=111105#src_doc_s2member_user_downloads%28%29
viewtopic.php?f=40&t=12453&src_doc_v=111105#src_doc_s2member_total_unique_downloads_of%28%29

Calculations returned by these functions do NOT include File Downloads that were accessed with an Advanced File Download Key.
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: counting downloads

Postby kennymcnett » December 2nd, 2011, 4:08 am

Thanks, Jason. I knew about those changes, and was excited until the disclaimer ;)

Maybe I'm missing something in my setup that would allow me to avoid using an Advanced Download Key?

Here's what I'm doing:

Each post has one or more links to files located in several subfolders of /s2member-files/. (For example, s2member-files/videos)

Users purchase access to a post on a per-post basis via a ccap buy now button (which dynamically assigns the ccap based on the post ID), or users can purchase a 1-year sitewide access via a level 4 membership buy now button.

The conditionals in my php only display the links to the s2member-files if they user has either the post's ccap or a premium membership.

As I understand the security for the s2member-files folder, because the files are in a subfolder, the only users that can access them via a direct url would be users who are logged in and possess a ccap equal to the subfolder name (e.g., 'videos'). This is good for me, because I don't want users sharing links with each other.

So, to grant access to the files, I am using an Advanced Download Key because it allows the user to access the file regardless of the restrictions.

Not sure if that scenario is clear, but I hope so. Any ideas?

I can DM you a user login to the dev site if that helps.

Thanks a million!
User avatar
kennymcnett
Registered User
Registered User
 
Posts: 23
Joined: October 5, 2011
Location: Los Angeles

Re: counting downloads

Postby Jason Caldwell » December 2nd, 2011, 4:35 am

Thanks for the follow-up.
The conditionals in my php only display the links to the s2member-files if they user has either the post's ccap or a premium membership.

As I understand the security for the s2member-files folder, because the files are in a subfolder, the only users that can access them via a direct url would be users who are logged in and possess a ccap equal to the subfolder name (e.g., 'videos'). This is good for me, because I don't want users sharing links with each other.
Sorry, just want to make sure I understand correctly. Can you elaborate just a bit further on this for me please? Did you say, "This IS good for me", or no? It sounds like you might not need the use of a Download Key, but just clarify a bit further so I can be sure please.
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: counting downloads

Postby kennymcnett » December 2nd, 2011, 6:16 am

Yes, it's a good thing. (I'm pretty sure...)

When a Level 0 user buys access to a post, they get a ccap equivalent to the post ID (e.g., post345). With that ccap, or if they are a Level 4 member, they can see the links in the post that were previously hidden (I do this by conditionally toggling the display of the content below the "More..." tag of the post).

Those links are to files in several different subfolders of s2member-files. For example
Link #1: s2member-files/videos/file.mov
Link #2: s2memberfiles/posters/file.jpg

The user does NOT have the ccaps of either 'videos' or 'posters'.

In testing, I was putting all of my downloadable files in the root of s2member-files, but it seemed that any logged-in user that had the direct URL to the file could access it. That's why I switched to using the Advanced Download Key--to obfuscate the direct URL in order to prevent link sharing among registered users who haven't paid for access to the post that houses those links.

Essentially, I aimed to lock down all of the files from any sort of user, and then use the Advanced Download Key to grant one-time access...because that was the only way I could figure out how to do it.

Happy to give more clarification if needed.

Thank you!
User avatar
kennymcnett
Registered User
Registered User
 
Posts: 23
Joined: October 5, 2011
Location: Los Angeles

Re: counting downloads

Postby Jason Caldwell » December 2nd, 2011, 6:46 am

Thank you for clarifying.

OK. So you really need the ability to use s2Member's API Functions for counting downloads, but since you're using a Download Key, that's not possible, given the current limitations in this regard. As I understand it, the only reason you're using a File Download Key right now, is to prevent link sharing among members that may not have a specific Custom Capability related to a specific Post/Page.

Just to clarify this ...
s2member-files/videos/file.mov
This file WOULD be available to anyone that meets your Basic Download Restrictions. Although it's in a sub-directory, it's NOT inside a special Custom Capability sub-directory. If you wanted to ensure that this file could ONLY be downloaded by a Member with a specific Custom Capability, you would upload it here:
s2member-files/access-s2member-ccap-videos/file.mov

Likewise, if you wanted to make a file ONLY available to a Member with Custom Capability post345, you would place the file here: s2member-files/access-s2member-ccap-post345/file.mov.

For further details on this, please check your Dashboard, under:
s2Member -> API Scripting -> Custom Capability and Member Level Files.

So, in your case, I would recommend creating sub-directories for each of the Custom Capabilities that you plan to sell, and then place files inside these Custom Capability sub-directories, so that link sharing is ONLY possible between one or more Members that both have this specific Custom Capability ( e.g. there is no security issue here, because the file is always going to require that a Member is logged in with a specific Custom Capability ). And, this will allow you to use s2Member's standard linking methods, as opposed to using a Download Key, which is prohibiting you from enjoying the benefits of counting downloads with s2Member's API Functions.

For example, upload your file here:
s2member-files/access-s2member-ccap-post345/file.mov
A link might look like this:
/?s2member_file_download=access-s2member-ccap-post345/file.mov

I suppose the next question is... how many different Custom Capabilities are you going to sell? Is it going to be practical to create Custom Capability sub-directories like this for each of them? If you're only selling 5-10 different Posts/Pages, I'd say it's well worth it, even if you have to duplicate certain files into some of these sub-directories to accomplish everything you need to.

On the other hand, if you're selling hundreds of different Custom Capabilities, you might get a headache with this ( i.e. too much work to create all of those sub-directories for each Custom Capability ). In that case, you might try to consolidate some of your most important Custom Capabilities a bit. For instance, you might sell Custom Capabilities "downloads_a", "downloads_b", "downloads_c". In other words, try to group them together a bit further to prevent the need to create a brand new Custom Capability sub-directory for each specific purchase. You could still sell Custom Capability post345, but it might also include access to "downloads_a". Anyway, just an idea.
~ Jason Caldwell / Lead Developer
& Zeitgeist Movie Advocate: http://www.zeitgeistmovie.com/

Is the s2Member plugin working for you? Please rate s2Member at WordPress.org.
You'll need a WordPress.org account ( comes in handy ). Then rate s2Member here Image
.
User avatar
Jason Caldwell
Lead Developer
Lead Developer
 
Posts: 4045
Joined: May 3, 2010
Location: Georgia / USA

Re: counting downloads

Postby kennymcnett » December 2nd, 2011, 7:53 am

Jason, thank you for your detailed and timely response.

If I use ccap folders as you've explained, will my Level 4 members be able to access those files? Or would they need to have that folder's ccap, too?

The ccaps are dynamically inserted into a auto-generated paypal button available on each post, so in theory there are infinite numbers of ccaps because there could be an infinite number of posts created. In practice, there will probably be 20-50 or so. Creating all of those folders might be manageable in order to solve the download counting dilemma, but only if Level 4 members can access those files, too, without needing the ccaps in their account. If they can't, then that kills it.
User avatar
kennymcnett
Registered User
Registered User
 
Posts: 23
Joined: October 5, 2011
Location: Los Angeles

Next

Return to s2Member Plugin

Who is online

Users browsing this forum: Yahoo [Bot] and 2 guests

cron