How to attach URL query parameters to a user during the sign up process. Possible?

I am trying to add a referral system to my project, so currently I am basing it off of this package. The issue I am running into is my project only uses accounts-google and not accounts-password. The way this package works is it adds the iron router query parameters for the referrerCode (/register?r=ReferralCodeHere)through a preSignUpHook. I believe this only works with accounts-password wont work when creating an account with an API such as accounts-google.

My idea around this is to use a Meteor.users.before.insert hook to grab the iron router query parameters and insert them into my referrerCode field in Meteor.users since Iā€™m already using Meteor Collection Hooks for a couple of other things.

The issue is I havent been able to find a way to get the query parameters on the server, I was hoping to do something like this:

Meteor.users.before.insert(function(userId, doc) {
  doc.referrerCode = Referrer._referrerCode; // Link 1 
});

(Link 1)

But this will just come up as undefined.

If Iā€™m at my register page and it has a query like this for example: example.com/register?r=12345 Then I run Router.current().params.query.r on the client it returns 12345. Basically I just need to have that saved to the referralCode field in Meteor.users when a new user creates an account, if a referral code exists in the register URL.

Iā€™m a bit lost with this one. I thought about setting it as a Session variable and then getting that in the before.insert hook, but that again only works on the client side. Iā€™m thinking a meteor method might be best for this, but Iā€™m not exactly sure how I would structure it.

Any help is greatly appreciated!

1 Like

If anyone has any ideas of another way that I can make this work Iā€™d definitely appreciate hearing them, nothing Iā€™ve tried seems to work and been stuck on this for a couple weeks now.

I basically just need a system so that users are able to have a referral code which goes in the register url (example.com/register?r=ReferralCodeHere), which they can then share and when new users sign up using their referral link it will be tracked. It seems like this package would work good for this but unfortunately that one and every other one Iā€™ve seen only works for accounts-password and not any API logins such as accounts-google (which is what my login system uses).

As I said above, the main issue seems to be getting the referral ID from the URL. This is easy to get using Iron-Router query parameters but that is only available from the client, so to get this during signup and attached to the user account is where Iā€™m quite lost. I was hoping to use a Meteor.users.before.insert hook but I still cant find a way to get the query parameters into the serverā€¦

Any ideas or anything at all is greatly appreciated, I feel like this should be much easier than itā€™s been :confused:

How about having a custom handler like this:

WebApp.connectHandlers.use("/url?var1=val1...", function(req, res, next) {

Then save these values somewhere that you reuse when the user logs in / creates account

Thanks for the reply!

As an update, Iā€™m not too familiar with connectHandlers, but Iā€™ve been reading up on it and trying a few things but havenā€™t been able to make anything work so far.

Iā€™ll continue to fiddle around with connectHandlers, but if anyone else has any suggestions Iā€™m also open to hearing them. This seems like it should be much easier than its been, 2 weeks of being stuck on this one little thing is pretty brutal, unfortunately itā€™s also an important step before I can continue to other areas of my project as wellā€¦

So I saw this a few days ago and it bothered me a bit that something that seems simple is causing issuesā€¦ Not sure this is the best way to do it, but the duct-taper in me thinks it may work?

  1. Check the docs below (form field config) for a way to add a hidden field to your atForm.
  2. In an onRendered, pull the referral code from your route and put it into your hidden field.
  3. Use Accounts.onCreateUser to grab the value from the hidden field and attach it to the user doc

Sources:
Form Field Configuration
Extending Meteor Users

Thinking some more ā€¦
when you use connectHandlers, use cookies as you donā€™t yet have a session. Then when you redirect to login / create user account, load up the cookies.

Iā€™m having basically the same issue. Itā€™s such a disappointment that this isnā€™t covered in the accounts-core/ui packageā€¦

I think the only way to do it is to roll your own signup form and use Accounts.createUserā€¦ otherwise there is no way to get a session variable to the serverā€™s Accounts.onCreateuser

I tried to store it in ā€˜infoā€™ in the preSignUpHook, then add the document in the postSignUpHook once the user is created and I have the userIdā€¦ but the preSignUpHook seems to wipe the session variable to itā€™s original stateā€¦ it comes back empty (I initialize it as empty in an Template.onCreated).

Iā€™ve come across this same use case probably 15 times but it doesnā€™t seem like whoever oversees accounts-core is interested in adding itā€¦ even though it could be a simple hook, instead anyone needing this is forced to roll their own.

I agree I would love to see a way built into accounts-core to make this process easier, itā€™s been quite frustrating and caused quite a delay. Thankfully I have everything working now, but the process isnā€™t super clean.

Iā€™d suggest checking out the referrals package which I was originally basing my system off of. The creator recently did an update making it possible to support social login. Itā€™s a pretty hacky solution but after the amount of time I was stuck on this itā€™s the only way I was able to make things work.

Hopefully weā€™ll soon see an update to accounts-core to solve this issue. There are loads of use cases and Iā€™m sure it would help a ton of people.

1 Like

So I implemented a roughly similar system. Instead of a referral argument, my system is keying off document.referrer and I save that to the userā€™s profile.

Implementation took me about 20-25 minutes. Iā€™m not using any hooks into the login system.

When someone loads up the website, I save the referrer to a client side variable when my header loads. After login, when the newly registered user loads the first authenticated page I will look at his user information. If there is no user.profile.referrer value (since this is not a hook but happens every load), I call a server function to write the previously saved referrer variable as the referrer and Iā€™m done.

Seems to work for me so far. Obviously my method has potential issues with someone refreshing a page. Iā€™ll see what % of my users have issues in time.

As the author of the super hacky package, I think I did try and make changes to accounts core at the time, but I think the issue was more on the meteor level of how accounts-facebook and other social login packages work. Meteor doesnā€™t give an option to pass in additional parameters when registering a user through social login.

One idea might be to create a wrapper method for loginwithfacebook and all the other social login methods. But I feel like that method could also end up being pretty hacky if you even managed to get that idea to work.

The best would seem to be to open an issue on the meteor github repo for loginwithsocial to receive an extra parameter of additional data to be added to the user document.