This is a follow-up post where I was explaining how to set/override GA campaign variables programmatically with JS.
The unique element of this problem is that all query string parameters are not passed along to your application’s iframe, so by default, we cannot use UTM campaign parameters. Otherwise stated: when GA campaign parameters are passed into your Facebook Page Tab URL, it will simply disregard it.
With some additional modifications, and understanding what parameters Facebook does pass in we are able to work around these limitations to get accurate campaign tracking.
The general idea is:
- Pass in a specific string that represents an “encoded” utm parameter string
- Use Facebook’s app_data parameter to pass in this specially encoded string
- Parse out the string in your application (server-side code)
- Take the extracted values and put them in your JS calls to “set” the campaign variables in GA
To further preface my solution, some general assumptions I am making/accounting for are:
- I want to ensure that many parameters can be passed in via app_data and not just our GA campaign params.
- The pipe (|) and double-pipe (||) chars are “reserved”. These are being used as delimiters in the app_data string to separate “main and sub-vars”.
- At various stages of the script, I am replacing the pipe chars with ampersands so I can simulate the creation of a query string and to leverage a native PHP function that parses this format
- There are alternative ways that I can structure my query strings for easier consumption, but I prefer this delimiter approach as it closely resembles the universal query string format.
The final url will look something like: https://www.facebook.com/pages/pagename?id=xxxxxxxx&sk=app_xxxxxxxxxxx&app_data=ga=utm_source=googly|utm_medium=hobo|utm_campaign=gdcampaign||otherparams=youdontwanttodisturb
Face book will take the app_data parameters and pass in in as a string. So the string we are eventually working with is: ga=utm_source=googly|utm_medium=hobo|utm_campaign=gdcampaign||otherparams=youdontwanttodisturb
In the first stage of parsing, you’ll have access to the “ga” along with the “otherparams” , but our main concern for now is the “ga” key – the second parameter was simply for demonstrative purposes on how to append other variables that are useful to your application.
Onto the commented code which should explain the rest of the technique:
- #ensure the facebook SDK is loaded
- $facebook=new Facebook($config);
- #get the signed request
- #if the signed request exists and has some app_data start parsing
- #expects a "pseudo querystring" of the format:
- #notice that the above string separates other values with double pipes || so you can have many querystring vars and not just GA
- #individual variables within each "key-value" are separated by single pipes |
- #replace the double pipes with an apersand - prepare the string for parsing
- #parse the querystring
- #replace the single pipes with an apersand - prepare the string for parsing
- #parse the querystring
- #escape the string values and prepare them for JS output
- #set some defaults and merge it with the ga params found. Just want to ensure all values exist in the array;
- (i[r].q=i[r].q||).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
- ga('create', 'UA-xxxxxxxx-x');
- <?php if(isset($clean_ga_params)) : ?>
- ga('set', 'campaignName', <?php echo $clean_ga_params['utm_campaign'] ?>);
- ga('set', 'campaignSource', <?php echo $clean_ga_params['utm_source'] ?>);
- ga('set', 'campaignMedium', <?php echo $clean_ga_params['utm_medium'] ?>);
- <?php endif; ?>
- ga('send', 'pageview');