Page 1 of 1

notify by email when file downloaded

PostPosted: June 11th, 2011, 3:44 pm
by sidera
Hi

is there a way to send an email to the admin whenver a file is downloaded?

thanks

Re: notify by email when file downloaded

PostPosted: June 12th, 2011, 12:02 am
by Cristián Lávaque
s2Member doesn't do that, but you could look in the source files and find a hook to use with a custom code that'd email you everytime a download happens.

Re: notify by email when file downloaded

PostPosted: June 15th, 2011, 4:22 am
by sidera
ok, can you point me to the file? thanks

Re: notify by email when file downloaded

PostPosted: June 15th, 2011, 7:17 pm
by Cristián Lávaque
Browse these, you'll probably find one that you can use for your customization viewtopic.php?f=40&t=9134&src_doc_v=110605 :)

Re: notify by email when file downloaded

PostPosted: June 15th, 2011, 7:25 pm
by Cristián Lávaque
Another idea would be to have the custom script that sends you the email be what the person clicks on and then forward him to the actual download.

1. Person clicks on link to your custom script.
2. Custom script sends you an email.
3. Custom script forwards person to the file.
4. s2Member serves the file.

Re: notify by email when file downloaded

PostPosted: June 16th, 2011, 9:32 am
by sidera
I managed to get an email sent with the ws_plugin__s2member_before_file_download_access hook, but not the ws_plugin__s2member_after_file_download_access

what if I want to redirect the user after the download to a thank you page, so Ican track the goal in google analytics?

Re: notify by email when file downloaded

PostPosted: June 16th, 2011, 11:08 am
by Cristián Lávaque
Hmm... I don't know how to do the redirection after download. :|

Re: notify by email when file downloaded

PostPosted: June 16th, 2011, 4:23 pm
by sidera
wherer can I get help with this kind of customization?

Re: notify by email when file downloaded

PostPosted: June 16th, 2011, 5:12 pm
by Cristián Lávaque
jobs.wordpress.net, oDesk, eLance, ScriptLance.

By the way, you may be able to use the ws_plugin__s2member_after_file_download_access hook for the redirection to the thank-you page you mentioned. viewtopic.php?f=40&t=9572&src_doc_v=110605#src_doc_line_238

I hope that helps. :)

Re: notify by email when file downloaded

PostPosted: June 16th, 2011, 6:26 pm
by sidera
yeah I tried using this hook because I believe it's the right one, however it's not being triggered??

Re: notify by email when file downloaded

PostPosted: June 17th, 2011, 5:25 pm
by Jason Caldwell
Thanks for the heads up Cristián.

Yea, this Hook is misleading, it's not the right one to use in this case. Sorry for any confusion in the name of this Hook, but this Hook is really only fired in special cases, which may occur whenever a download attempt is being re-routed in some way; not useful in this case though.

In fact, I wouldn't Hook into the s2Member download routine at all ( at least, not to perform a redirection ), because it could interfere with a successful delivery of the file itself. Instead, you might consider spawning a new window, or doing something else creative with the link itself. For instance, with Google Analytics, I recommend one of these methods:

Reference articles:
http://www.google.com/support/analytics ... swer=55529
http://www.google.com/support/googleana ... swer=55527
http://www.goodwebpractices.com/roi/tra ... cally.html

All of this being said, we are planning to create an API Notification for File Downloads, which will make new things possible, such as automatic email notifications and custom scripts that can receive the event as well. Look for this come to come in a future release. Until then, if you'd like to add this type of funcntionality yourself, you might try this Hook: ws_plugin__s2member_during_file_download_access

Also see documentation:
viewtopic.php?f=40&t=9134&src_doc_v=110605#src_doc_ws_plugin__s2member_during_file_download_access

Re: notify by email when file downloaded

PostPosted: June 17th, 2011, 11:28 pm
by Cristián Lávaque
Awesome! I learnt a few new things with those articles on Google Analytics. Thanks. :)

Re: notify by email when file downloaded

PostPosted: June 18th, 2011, 4:39 am
by sidera
thanks Jason,

I'll use one of those methods instead of the redirect.
the mail notification works with the hook you mentioned.

I have another question : what data is available during that event, to send in the email, like member name, file name, etc...?

thanks!

Re: notify by email when file downloaded

PostPosted: June 20th, 2011, 3:36 am
by Jason Caldwell
You're very welcome.

There are many variables available with this Hook. As seen in the source code, you get all PHP variables defined within the scope of the Hook, at the precise point in which the Hook is fired by s2Member. Looking at the source code is probably the easiest way: viewtopic.php?f=40&t=9572&src_doc_v=110605#src_doc_line_175

Note how s2Member passes you the value of get_defined_vars(), as explained here in the Hook example: viewforum.php?f=40#src_doc_overview_description

Here is a quick example with some of the most useful variables:
Code: Select all
<?php
add_action
("ws_plugin__s2member_during_file_download_access", "my_function");
function my_function($vars = array()){
    $user_id = $vars["user_id"];
    $file = $_GET["s2member_file_download"];
    $user = new WP_User($user_id);
    //print_r($user);
}
?>

Re: notify by email when file downloaded

PostPosted: June 20th, 2011, 2:27 pm
by sidera
ok, so I did a print_r, and it seems all the variables I need are there, but I'm not sure how to use them, this gives me nothing :
Code: Select all
$first_name = $vars["user->first_name"];
   $last_name = $vars["user->last_name"];


this is part of the var dump
Code: Select all
[user] => WP_User Object ( [data] => stdClass Object ( [ID] => 1 [user_login] => paul [user_pass] => secrett. [user_nicename] => paul [user_email] => secret [user_url] => [user_registered] => 2011-06-02 19:05:22 [user_activation_key] => [user_status] => 0 [display_name] => paul [first_name] => [last_name] => [nickname

Re: notify by email when file downloaded

PostPosted: June 20th, 2011, 10:20 pm
by Cristián Lávaque
First and Last names seem to be empty in that array. Could you try $user->user_login?

Re: notify by email when file downloaded

PostPosted: June 21st, 2011, 2:58 pm
by sidera
fixed it :

Code: Select all
$user_info = $vars['user'];
$user_display_name = $user_info->display_name;