In the first blog I showed you how to add the ADTELLIGENCE “tracking pixel” from our Social Analytics+ tool, in the second blog I showed you how I added a WordPress Plugin to access Facebook through the blog. Now in the third I’m going to explain why the second blog post was wrong and what I did to get a true “Facebook Connect” into the blog and then how I modified the “tracking pixel” to work with the Facebook data.
I’ve been writing these blogs from the perspective of someone who has no idea about our tool as I wanted to show how easy it was to add but also try to catch and address some of the basic questions that might arise. I hit the first of those questions yesterday when I realized that my efforts in the second blog were 1) a learning experience but 2) in vain.
Facebook has made life easy for developers who want to incorporate Facebook and various websites and in the last blog what I settled on was actually a “Social Plugin” and not the Facebook Connect that I really needed. Now it should be said that with Facebook Connect you are in this case actually adding a Facebook User to your WordPress blog. So ensure that “anyone can register” and that a registered user gets the role of “subscriber” initially.
Next you’ll want to deactivate the plugin (if it’s the same one I added) since the new one I found and have tried now seems to work. If not it will kill your WordPress install since they will conflict with each other.
Remember the whole point here is to get more than just the basic data, it’s to get the social data.
So here we go off and running, I once again began searching for a proper plugin. Yes at this stage I did considering building the whole FB Connect integration myself but hey there are plugins so why not…
What I finally decided on was the following “Facebook AWD All in One“, it seems to work and I was able to get the Facebook user so I knew Facebook Connect was working.
Configuration was a bit of fun though as there are several other items one has to consider when doing this.
First you needed to do the Facebook APP ID and Secret like before, and the various other preferences by choice. The one new aspect though was the API permissions. Now these are very important. You need to alert your users as to what information from Facebook you are asking them to share with you. Regardless of what you ask though it’s still only based on what they share with everyone, privacy is important after all.
To find out the choices of permissions you can visit the Developer section on API Permissions. For Our “Social Analytics+” tool there are a few things you need to ask for in order to send the data to our tool. We will update you as there are changes in the works for additional data as well.
- user_about_me – gender, bio, name, the basics
- user_likes – what they like
- user_birthday – their age
- email -Our tool does not but WordPress does need the ability to email users with comment notifications and such.
Now it’s time to get back to the code, if you remember from the first and second blog we had modified the “header.php” of the WordPress install to get the “tracking pixel” in place. Well to make life a bit easier we are going to move it from the “header.php” and place it in the “footer.php” instead.
So head over to “Appearance” from the admin login on your blog and choose the “editor” option. Find the “body” tag in “header.php” and remove the code we added. Now update that file and choose the “footer.php“.
For me I decided to add the code right after the “DIV” tag for the foot, basically the end of the file.
The basic’s are the following:
<?php
$companyid = '#######';
//get the facebook AWD object
global $AWD_facebook;
//get the facebook icon if exist and if activated and if there is a facebook account
if($AWD_facebook->plugin_option['connect_enable'] == 1){
$fbuid = get_user_meta($user_ID,'fb_uid', true);
if($fbuid != ''){
echo '<img src="http://socialanalytic-1085123149.eu-west-1.elb.amazonaws.com/socialanalytic-web-rest/rest/action/'.$companyid.'/'.$fbuid.'/p1/s1" border="0" height="1" width="1">';
}else{
echo '<img src="http://socialanalytic-1085123149.eu-west-1.elb.amazonaws.com/socialanalytic-web-rest/rest/action/'.$companyid.'/'.time().'/p1/s1" border="0" height="1" width="1">';
}
}
?>
However all this does is change the unique User ID, to capture the “social” data we now have to hack a little more into the plugin to get the “access token” to then be able to access more of the user’s social data.
<?php
/*
* ADTELLIGENCE GmbH Social Analytics+
* (C) 2011 ADTELLIGENCE GmbH
* info@adtelligence.de
<ns0:UserAddRequest xmlns:ns0="http://www.adtelligence.de/socialanalytic/webservice/">
<id>500064793</id>
<birthday>1976-02-16</birthday>
<gender>male</gender>
<Interests>
<interest id="20950498489">
<name>Girl With The Dragon Tattoo</name>
<category>Book</category>
</interest>
</Interests>
</ns0:UserAddRequest>
*/
$adt_user = 'xxxxxxxxxx';
$adt_pwd = 'xxxxxxxxxx';
$companyid = 'xxxxxxxxx';
$url = 'http://socialanalytic-1085123149.eu-west-1.elb.amazonaws.com/socialanalytic-web-rest/rest/user/'.$companyid;
//get the facebook AWD object
global $AWD_facebook;
//get the facebook user ID and user info
if($AWD_facebook->plugin_option['connect_enable'] == 1){
$fbuid = get_user_meta($user_ID,'fb_uid', true);
if($fbuid != ""){
$user = json_decode(file_get_contents('https://graph.facebook.com/'.$fbuid.'?access_token='.$AWD_facebook->fcbk->getAccessToken()),true);
$xml_data = '<ns0:UserAddRequest xmlns:ns0="http://www.adtelligence.de/socialanalytic/webservice/">'.
' <id>'.$fbuid.'</id>'.
' <birthday>'.substr($user['birthday'],7,4)."-".substr($user['birthday'],0,2)."-".substr($user['birthday'],4,2).'</birthday>'.
' <gender>'.$user['gender'].'</gender>'.
' <Interests>';
$likes = json_decode(file_get_contents('https://graph.facebook.com/'.$fbuid.'/likes?access_token='.$AWD_facebook->fcbk->getAccessToken()),true);
for ($x = 0; $x < count($likes['data']); $x++) {
$xml_data .= ' <interest id="'.$likes['data'][$x]['id'].'">'.
' <name><![CDATA['.$likes['data'][$x]['name'].']]></name>'.
' <category>'.$likes['data'][$x]['category'].'</category>'.
' </interest>';
}
$xml_data .= ' </Interests>'.
'</ns0:UserAddRequest>';
/** use a max of 256KB of RAM before going to disk */
$fb = fopen('php://temp/maxmemory:256000', 'w');
if (!$fb) {
echo 'could not open temp memory data';
exit();
}
fwrite($fb, $xml_data);
fseek($fb, 0);
$adt_ch = curl_init($url);
curl_setopt($adt_ch , CURLOPT_VERBOSE, 1);
curl_setopt($adt_ch , CURLOPT_HEADER, 0);
curl_setopt($adt_ch , CURLOPT_RETURNTRANSFER, 1);
curl_setopt($adt_ch , CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($adt_ch , CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($adt_ch , CURLOPT_USERPWD, "$adt_user:$adt_pwd");
curl_setopt($adt_ch , CURLOPT_PUT, 1);
curl_setopt($adt_ch , CURLOPT_BINARYTRANSFER, 1);
curl_setopt($adt_ch , CURLOPT_INFILE, $fb);
curl_setopt($adt_ch , CURLOPT_INFILESIZE, strlen($xml_data));
if (($result = curl_exec($adt_ch )) === FALSE) {
echo 'cURL error: '.curl_error($adt_ch )."<br />\n";
}
$resultArray = curl_getinfo($adt_ch);
echo "HTTP_CODE:".$resultArray['http_code'];
fclose($fb);
curl_close($adt_ch );
echo '<img src="http://socialanalytic-1085123149.eu-west-1.elb.amazonaws.com/socialanalytic-web-rest/rest/action/'.$companyid.'/'.$fbuid.'/p1/s1" border="0" height="1" width="1">';
}else{
echo '<img src="http://socialanalytic-1085123149.eu-west-1.elb.amazonaws.com/socialanalytic-web-rest/rest/action/'.$companyid.'/'.time().'/p1/s1" border="0" height="1" width="1">';
}
}
?>
You’ll need to of course update/edit the lines (20-22) for “User”, “Pwd” and “CompanyID” these were sent to you via the initial registration email. The “CompanyID” can actually be found in the url after the “/action/” right before we added the timestamp or FB user ID.
Update the file and you should be off and running…























