Indicating intent with omniauth facebook - ruby-on-rails

I'm using the omniauth-facebook gem and would like to pass Facebook a string indicating whether the user is trying to join or login, so that I can use the information when they return to my site.
It would be great if I could do something like this:
= link_to 'Join with facebook', '/auth/facebook?intent=join'
= link_to 'Login with facebook', '/auth/facebook?intent=login'
In case anyone wants to know why I want to do this, here's what I plan to do with the information:
# pseudo code
- if authentication fails
- if user was trying to join using facebook
= return them to the signup url
- elsif user was trying to login using facebook
= return them to the login url
So is it possible to signal my intent to facebook, and have them return that intent with their response so that I can use it if I need to?
I'm not using Devise, and would prefer to avoid having to set session variables to do this, Facebook must surely have a solution for this need...

Looks like this is possible and my code does work. When I inspect the response from facebook, I see the following:
>> request.env['omniauth.auth']['intent']
=> nil
# no dice
>> params
=> returns lots of stuff, but not the thing I want
# worth a try
>> request.env['omniauth.params']
=> {"intent"=>"login"}
# bingo
So the params hash that rails puts together does not contain the desired info, but the response does contain a bunch of unsigned details, and this is where the information I want can be found.
Worth noting you can also call:
>> request.env['omniauth.origin']
=> "http://localhost:3000/join"
Pretty cool.

Related

Analytics UTM Tags - Data not recording

I created a helper module in Rails that creates a hash of parameters that are passed to link_to rails helpers. These utm parameters are being used in emails sent out by the Rails app. The problem is that Google Analytics is not picking up any data when I've been testing. I realize there is a delay in processing and I'm also using the GA debugger to look at the beacons it's actually sending out and I have a staging server and staging Google Analytics that I am testing all of this on. Does anyone see anything with the approach below that would cause GA to not pick up any visits under the "campaigns" report? Does the order of the utm tags actually matter? I realize utm_campaign, utm_source and utm_medium are all required and ensured they are in every link.
For example, this is what one of the links looks like. However, GA is not picking up any data.
http://example.com/?utm_campaign=welcome&utm_medium=email&utm_source=abc
I compare this link that that the link_to method has created with to what the Google UTM Link Builder outputs and the only difference is the order of the parameters. Does the order matter?
http://example.com/?utm_source=abc&utm_medium=email&utm_campaign=welcome
Here is the helper module and an example of it's usage with a link_to.
# mailer_helper.rb
require 'uri'
module MailerHelper
def self.tracking_param_hash(utm_source, utm_medium, utm_campaign, params = {})
{ utm_source: utm_source,
utm_medium: utm_medium,
utm_campaign: utm_campaign
}.merge(params)
end
def self.email_tracking_params(utm_campaign, options = {})
tracking_param_hash("abc", "email", utm_campaign, options)
end
...
end
# example usage in email view
email_params = MailerHelper::email_tracking_params("set-password", reset_password_token: #token)
link_to 'Set my password', edit_password_url(#resource, email_params)
# rendered link in email
http://example.com/users/password/edit?reset_password_token=YEt9PJgQsxb3WpJQ7OEXH3YDT8JZMD&utm_campaign=reset-password&utm_medium=email&utm_source=abc
Data wasn't recording for several reasons. The first one was because one of the link users were clicking on was doing a POST to a user_confirmation_path. Because the urchin was never loads on the POST, no data will be recorded and users are re-directed to the sign in page. The parameters need to persist with the re-direct from the POST, otherwise traffic will be treated as direct. Near the end of this Kissmetrics blog post they outline this problem. To get around this, you can pass parameters into the redirect url. So something like redirect_to user_signin_path(tracking_parameters), where tracking_parameters are the utm tags from the POST URL.
The second was because the utmz cookie was persisting across and wasn't counting the visit as a new session. You need to test everything in a fresh incognito window because of how Google Analytics and chrome treat individual sessions. See this SO post for more details.
Finally, for anyone who reads this, you can confirm that UTM parameters are being set by looking fora cookie called utmz. This cookie is used by Google Analytics and will look something like this 12340657.1412743014.15.1.utmcsr=abc|utmccn=confirm|utmcmd=email and should have the utm parameters in it.
[Edited for clarity and and the various scenarios I was testing]
According to google, the order doesn't matter. AFAICT everything is in-order in your code, so the problem is somewhere else. Cookies?

Fetching FB pages of a user using Koala

I have an FB account and i have created a fan page for my club and few other pages as well. I have an app in Ruby on Rails. I want to publish some feed on my club fan page. How can i do this?
I have been using Koala Gem and able to successfully post to my wall but not on to the page.
I want to access the list of all the fan pages associated with my account instead of giving the name of specific page.
here is my simple method which i am using to communicate to FB Graph API.
def facebook
#facebook ||= Koala::Facebook::GraphAPI.new(oauth_token)
rescue Koala::Facebook::APIError => e
logger.info e.to_s
nil # or consider a custom null object
end
Answer submitted by Sumit can be an approach but after searching around some more forums, finally i got an elegant way to do it.
#user_graph = Koala::Facebook::API.new(user_access_token)
pages = #user_graph.get_connections('me', 'accounts')
# get access token for first page
first_page_token = pages.first['access_token']
# or: retrieve access_token for a given page_id
page_token = #user_graph.get_page_access_token(page_id)
Passing on the "accounts" parameter to get_connection worked elegantly for me.
Here is the reference to API.
And one last thing, never forget to add the "manage_pages" permission in your permissions list.

Rails 3 Linkedin API gem

I'm trying to set up the linkedin api in a rails 3 app using the linkedin gem. I don't want the user to have to authenticate my app in order for the API to get their info. I only need one piece of their public profile (the headline). So, maybe I should just be using xml or json to pull this off (not exactly sure how to get that with linkedin either).
I have the following in a helper so that I can call linkedin_header() in a loop of users. I only have 'client' as the last line of the following code while debugging. It outputs as expected (#). It seems like I am only a step away from success. How can I access a given users headline? I have tried using "client = client.profile(:url => 'linkedin_user_url')", but that return "Call must be made on behalf of a member".
def linkedin_header(account_user)
user = User.find(account_user)
account = Account.where(:user_id => user, :external_id => 1)
api_key = 'aaaaaaaa'
api_secret = 'bbbbbbbb'
client = LinkedIn::Client.new(api_key, api_secret)
rtoken = client.request_token.token # this returns correctly
rsecret = client.request_token.secret # this returns correctly
client
# client = client.profile(:url => 'linkedin_user_url')
end
So, I guess I have two questions. Is my request (public headline of any user) too simple for the above...should I be using XML or JSON. And, if Im close...can I make the API work for me without the user having to authenticate via linkedin.
Based off of what I read from the LinkedIn API reference (http://developer.linkedin.com/documents/authentication)
You have to make requests to their API only after being authenticated. (Using OAuth Keys) Rather than just grabbing the publicly available information.
It seems like since you want a small piece of information (the public headline of any user) you'd want some sort of implementation like Facebook's OpenGraph. After looking around on LinkedIn, I don't see any sort of public implementation like that.
I would suggest checking out this gem:
https://github.com/yatishmehta27/linkedin-scraper
It seems to be the type of solution you're looking for.

Facebook app: within Canvas, Facebook treats GETs as POSTs?

In my Rails FB app, when running in the canvas, if I go to "/users", instead of GETting the index page, it POSTs and creates a new user.
That only happens when I try to access the URL directly. When I use the "List of users" link on the app, it works as expected.
Why is that?
that's just how FB does it. They post data with each query as well.
Facebook sends everything as POST which brakes RESTful routes. There is a way to fix it though. If incoming POST request contains signed_request parameter you can assume it was converted from GET to POST by Facebook.
Rack::Facebook::MethodFix middleware fixes the problem automagically. You can use it with something like:
# Basic usage
use Rack::Facebook::MethodFix
# Also validate signed_request
use Rack::Facebook::MethodFix, :secret_id => "c561df165eacdd6e32672c9eaee10318"
# Do not apply request method fix to admin urls.
use Rack::Facebook::MethodFix, :exclude => proc { |env| env["PATH_INFO"].match(/^\/admin/) }
or if you are using Rails then
config.middleware.use Rack::Facebook::MethodFix

Difficulty getting all pages a user likes

I use this code, it get all pages the user likes:
#user = session[:graph].get_object('me')
like = session[:graph].get_connections("me", "likes")
if !(like.to_s.include?('appid'))
redirect '/youneedlike'
end
It's works for me and my mates, but some users have error: they are always redirected, even though they like our page.
What's wrong?
Does your app request the user_likes permission?
Alternatively, if your app is a canvas app, you can use the signed_request parameter to do the same thing:
# pseudocode
signed_request = decode signed_request()
if signed_request['page']['liked']:
# user liked page, do something cool
else:
# user doesn't like page. redirect somewhere to tell them why they should

Resources