JavaScript Quick Start Guide
To accommodate cross-domain security policies, awe.sm provides a JSONP callback when the callback parameter is specified. Many JavaScript libraries automatically add the callback parameter when a JSONP response is declared as the response type.
We're using jQuery for these examples, but you can use whatever JavaScript library you prefer or perform calls natively. The examples log to your browser's JavaScript console. (More: Safari, Firefox, Internet Explorer, Chrome)
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
Creating A Link
In this example, the demo tool is creating a link intended to be shared on Twitter that redirects to http://developers.awe.sm/.
$.ajax({
type: "POST",
url: "http://api.awe.sm/url.json",
data: {
v: 3,
url: "http://developers.awe.sm/",
key: "5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9",
tool: "mKU7uN",
channel: "twitter"
},
dataType: "jsonp",
success: function(newLink) {
console.log(newLink.awesm_url);
}
});
Output: http://demo.awe.sm/KD7
Retrieving Stats On A Link
Later, the stats for this link could be retrieved:
$.ajax({
type: "POST",
url: "http://api.awe.sm/stats/demo.awe.sm_KD7.json",
data: {
v: 3,
url: "http://developers.awe.sm/",
key: "5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9"
},
dataType: "jsonp",
success: function(linkStats) {
console.log(linkStats);
}
});
Output:
{
"awesm_id": "demo.awe.sm_KD7",
"clicks": 2,
"end_date": null,
"interval": "none",
"start_date": null,
"with_conversions": false,
"with_metadata": false,
"with_zeros": false
}
Retrieving Aggregated Stats
Let's find out how many times http://developers.awe.sm/ has been shared and how much traffic social sharing created.
$.ajax({
type: "POST",
url: "http://api.awe.sm/stats/range.json",
data: {
v: 3,
url: "http://developers.awe.sm/",
key: "5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9",
original_url: "http://developers.awe.sm/"
},
dataType: "jsonp",
success: function(pageShareStats) {
console.log(pageShareStats.totals.clicks + " clicks from " + pageShareStats.totals.shares + " shares");
}
});
Outputs: 109 clicks from 5 shares
Let's find the top three social networks (service) shared to in September 2011.
$.ajax({
type: "POST",
url: "http://api.awe.sm/stats/range.json",
data: {
v: 3,
url: "http://developers.awe.sm/",
key: "5c8b1a212434c2153c2f2c2f2c765a36140add243bf6eae876345f8fd11045d9",
group_by: "service",
sort_type: "shares",
per_page: 3,
start_date: "2011-09-01",
end_date: "2011-10-01"
},
dataType: "jsonp",
success: function(serviceShareStats) {
console.log("#1: " + serviceShareStats.groups[0].service);
console.log("#2: " + serviceShareStats.groups[1].service);
console.log("#3: " + serviceShareStats.groups[2].service);
}
});
Output:
#1: twitter #2: facebook #3: email
Last updated 2011-10-12