Ok So here's the setup
Sending FB ads to Page tab
Trying to track 1 extra variable ( ad id, demo etc )
Tracking platform cpvlab
Can it be done? Can I pass one extra variable? Old thread I found from 2011 says it cannot, has it changed in a year, seems with sponsored story it can by adding url values, but am looking for regular ad.
Possible?
//WARNING: I am sitting around at LAX (8hr layover) so prepare for something lengthy.
To pass some values to a FB page tab app or any app you need to pass these with the app_data querystring, i.e. URL has to have ?app_data=xxxx in it. Facebook passes this data through the iFrame (as JSON that you have to decode/split apart) allowing the page inside to collect and use it, which requires using a signed request. You can pass as many variables as you want, limited by overall characters in the string. I think you can only do this for a page ad via an API client, but you can edit the URL of an app advert normally. Might have to use power editor, FB is changing their system so often shit just disappears, I don't use it so dunno.
So for each page tab app advert you could append a specific &app_data=XXX value and pass that through for tracking. Also for page post ads you can do some funky stuff with replacing tokens in the post's URL but I don't think it's what you're after.
Here's a quick tut and some scriptin shizzle from my vault:
Create an app advert and append tracking variables to URL in the following form:
http://www.facebook.com/pages/pagename/123456789012345?sk=app_PAGE_APP_ID&app_data={"vari able1":"value1","variable2":"value2","variable3":" value3"}
The app_data string is in JSON form and MUST MUST MUST be correctly written. If it is not it will not work. Quotes around first variable name, then a colon, then quote marks around value, then a comma, repeat. No spaces. No Mistakes.
This app_data stuff will be passed into the page tab iFrame so your page inside can get all of that data, decode it, then split it up into parts hoooray for you to tack on to some affiliate/tracking URL. How to do that? First you need to retrieve a signed_request that FB passes, otherwise you can't get this data from FB. Then you decode it, get the JSON baby (the array of variables), then you explode it into little parts and do some magics.
[PHP]
<?php
$signed_request = $_REQUEST['signed_request']; // Get the signed_request data that FB passes (lots of shit)
$pre = explode('.',$signed_request); // Get the part of the signed_request we need.
$json = base64_decode($pre['1']); // Base64 Decode signed_request making it JSON again.
$obj = json_decode($json,true); // Split the JSON into arrays.
$app_data = json_decode($obj['app_data'],true); // Get out just the app_data part
$variable1 = $app_data['variable']; // Pull out the value attached to our first variable in the array and set it to something for later use.
$otherparameter = $app_data['parameter name']; //And so on..
[/PHP]
Now, if I was you, I would make your page tab content some script that has all this in there and in the body echoes the variables so you can create an advert > preview > test it is working, play around etc. If I recall rightly FB also gives your page tab access to a small amount of user data like gender?, age range and country. You should be able to see these if you just echo the entire $signed_request or $app_data, bleh I dunno one of those. Anyway these may come in handy, useful when people come not via adverts so you can at least get some info.
To do this you can decode everything then use if(isset($variable)) to check if someone has some value you passed via app_data, and if they don't, you just set everything differently.
[PHP]
$pre = explode('.',$signed_request);
$json = base64_decode($pre['1']);
$obj = json_decode($json,true);
$app_data = json_decode($obj['app_data'],true);
$user = $obj['user']; //array of some user data
$age0 = $user['age']; //ignore
$age = $age0['min']; //age range methinks
$country = $user['country'];
[/PHP]
Can't remember how you get gender. This is getting way too long winded and I'm soon to leave LAX so I will call it quits here. Enjoy playing with PHP.
@zeno... you're the man!!! you should be elected!
Elected president of New Zealand, and super governor of Ostraliya right? I had this shizzle setup for some fanpage campaigns + dynamic split testing with Clicky. Was testing passing of advert tracking data and subids for BuyBuddy so there was some intense coding going on. Looking back it's hard for even me to figure out my site files, lols. But what I had was a single fanpage tab which had split testing of landers + subids passed for tracking in the lander + redirection to generic page if they came organically, with general user data then pulled to fill in the tracking gaps/collect data.
zeno is so hardcore with his php skills
I'm in awe. wow.
I'm using "Static Iframe Tab" for my FB page - will this code work with it or do I need to create a page tab using some other method?
@zeno, thanks - I did try putting some php code into the "Static Iframe Tab", but saw no output.
Anyone have a tutorial on creating a page tab app? Just want to be able to have a page tab that can support PHP code.
Making page tabs is easy as! As for containing PHP code, you just point them directly at whatever URL you want. To make a page tab app you do this:
1. Go to https://developers.facebook.com/apps
2. Click "Create New App"
3. App name whatever you want, leave namespace blank and hosting option unticked -> click continue.
4. It will take you to your app config page where you can change things. Click the [TICK] Page Tab box section at the bottom of the page.
5. Fill out a page tab name (will show as tab name on your page)
6. Put in the URL of the page you want to load in the tab canvas and the same in the secure URL but obviously with https instead of http.
7. Make sure tab width is on normal (810 px). SAVE CHANGES. You can change the tab images later. The page tab image shows on your main page underneath the cover image and is 111x74. The main tab icon up the top is 75x75 and the small icons (can see these in your apps list) are 16x16. Lets add that page tab app to your page!
8. Copy and paste this into your address bar:
https://www.facebook.com/dialog/pagetab?app_id=IDHERE&next=PAGEURL
Damn son, you sure you're not getting your PhD in computer engineering?
I followed Zeno's easy-to-follow page tab creation steps - does anyone know if it is possible to extract and then echo out the user's age, gender, etc?
Some of that info is in the signed request FB passes through to the tabs. Use
[PHP]
$pre = explode('.',$signed_request);
$json = base64_decode($pre['1']);
$obj = json_decode($json,true);
$app_data = json_decode($obj['app_data'],true);
echo $app_data
[/PHP]
You should be able to find that info inside the app_data array of information, then you just extract it out.
Hi Zeno,
Thanks for the code - I tried but the $app_data is empty if I click on the page tab directly. If I come from one of my adverts (http://www.facebook.com/pages/pagename/123456789012345?sk=app_PAGE_APP_ID&app_data={"vari able1":"value1"}), the echo $app_data just displays "Array".
Oh, my bad. Should've thought this one through more. Try printing out the $obj array, I think that contains all the interesting data whereas app_data is just what you pass specifically. Use var_dump($obj).
Hi Zeno, thanks, that worked - I was trying to echo $obj but was also just getting "Array". The var_dump($obj) does contain some interesting info. Thank you again.
Hi Zeno,
Instead of an app advert in Facebook, can we paste those php code into a external url, or Landing Page (http://www.mydomain.com/index.php) and still get those JSON string from Facebook? and pass on to a redirect.php to Prosper 202 for tracking?
Sutarjo
Yes, you can use an external ad. The URL will just have to look something like this:
http://www.facebook.com/pages/something/1234567890?sk=app_1234567890&app_data={"subid1":"XX","subid2":"YY"}