I'm trying to create a rule that will flag a user who was referred by an affiliate when she registers and has subscriber role on behalf of the affiliate. The goal of this is to list all users referred by an affiliate and calculate the commission with views.

I tried the rule below but users didn't get flagged. I will appreciate some help. Thanks.

Event:
User has logged in
Condition:
User has cookie with affiliate_nid and campaign is active
Action:
(1) Add a variable:
Parameter: Type: Affiliate campaign
Provides variables: Added variable (variable_affiliate)
(2) Flag a user:
Parameter: Flag: Affiliate referred user, user: [account], User on whose behalf to flag: [variable-affiliate:affiliate], Permission check: false

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

mario_prkos’s picture

I needed something similar. I used user reference for marking referral. I also, did some small amount of coding. I built action that perform saving user references to user profile.
In file my_module.rules.inc

/**
 * Implementation of hook_rules_action_info().
 */
function my_module_rules_action_info() {
  return array(
    'my_module_save_refellar' => array(
      'label' => t('Save referral info to profile'),
      'group' => t('My module'),
      'parameter' => array(
        'user' => array('type' => 'user', 'label' => t('User that is logged in.')),
      ),
    ),
    
  );
}

/**
 * save refellar to user profile form cookies
 * @param type $user
 */
function my_module_save_refellar($user){
  $referral = $_COOKIE['affiliate_ng']['affiliate_uid'];
  if (empty($user->field_referral['und'][0]['target_id'])){
    $user->field_referral['und'][0]['target_id'] = $referral;
    user_save($user);
  }  
} 

Then I created rules:
event: user logged in
condition: Check cookie: affiliate_uid = [site:current-user:uid]
condition: User has cookie with affiliate_nid and campaign is active.
event: save referral
I created entity reference field in user profile that points to his referral .
Using Order views and relationship to referral field you can list who buy what and to whom is users related as referral.

mario_prkos’s picture

Issue summary: View changes

formatted

Perignon’s picture

Issue summary: View changes

This to me seems very valuable. I am just trying to think where it should go/reside. I also maintain the module Commerce Rules Extra. Maybe this could have a home there or as a submodule under Affiliate NG. There would need to be some controls over the field attached to the user entity though the UI. But the above code can be used for anyone that would need this functionality immediately. The primary focus for Affiliate NG right now is getting some usable reports of the data being recorded.

deggertsen’s picture

I will probably be needing this functionality as well. Concerning where the extra functionality should be added, I personally think it should be a submodule under Affiliate NG called something like "Affiliate User Relationship" or "Affiliate User Link". This is what I see the module needing to do:

  1. Create new user relationship field on install (make sure removal of this field is setup for when this module is uninstalled)
  2. Create a rule that would trigger on new account creation and use the action mario_prkos wrote in comment #1

I wonder if the title of this issue should be renamed to something like "Permanently link users referred by affiliates".

deggertsen’s picture

Title: Flagging users referred by affiliates in rules » Create permanent User->Affiliate relationship using rules
Assigned: Unassigned » deggertsen
Category: Support request » Feature request
Status: Active » Needs review
FileSize
9.51 KB

I did it. Here's a patch with the new module "Affiliate Relationship". Check it out and give me your feedback.
Thanks to mario_prkos for his code in #1.

deggertsen’s picture

Anyone else willing to look at this?

deggertsen’s picture

I found a few problems with my last patch and have fixed them with this one. I'm now using this on a production site.

deggertsen’s picture

Oops. Forgot to define the new rule parameter in the default rule. Hopefully that will be it!

george2u’s picture

Hi deggertsen , thanks for creating this, it seems to be something I am after too.
However when I tried to get the 'relationship' module, the module page says it is abandoned and suggested to use 'relation' module instead?

deggertsen’s picture

@george2u, I'm not sure what 'relationship' module you are referring to (link would help), but I'm talking about the one that should be created with the patch in #7 called "affiliate_ng_relationship" that is part of this same affiliate-ng module project. I'm not talking about the 'Relationship' module if that's what you are talking about as indeed that module is no longer supported.

Does that make sense?

Perignon’s picture

Minor change. Use a different tag in the view other than "default". I have a ton of views, have to use tags :-D

Perignon’s picture

I been working with this tonight and I see one very big hole in this module. This saves affiliate information only if the user has logged in and editing their account.

Let's back up and track the user registration. You are missing a valuable step in a digital funnel, tracking user registration on your site. What if someone does not get the activation email? You have missed a valuable piece of data to react to in your business logic. I want to reach out to accounts that have registered from an affiliate and see if there was some problem.

deggertsen’s picture

@Perignon, would we add another action to be triggered on creation of a new user account? What would you suggest? I agree with your point that it is missing an important part. For my use case I actually only need it for login, but I think having an action at registration would be good.

Perignon’s picture

I actually got it to work. Forgot to come back and post here. If you add the action "After saving a new user account" to the Events portion it works for both!

deggertsen’s picture

Lovely! I thought that might be the case. Would you say this is ready to commit?

Perignon’s picture

Yeah put it in dev. It's a small submodule. I am looking at making another submodule that does the same thing to an order object.

  • deggertsen committed 7da192a on 7.x-1.x
    Issue #1869620 by deggertsen, Perignon: Create permanent User->Affiliate...
deggertsen’s picture

Status: Needs review » Fixed

We should probably create some documentation for this sub-module somewhere, but I've committed it anyways. I'll open a new issue concerning documentation.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.