1.0.0 • Published 8 years ago
gapi-url v1.0.0
gapi-url 
Small wrapper around Google Url Shortener API providing 'insert' and 'get' methods.
const gapiUrl = require('gapi-url')
const API_KEY = '<your api key here>'
// Shorten a URL
shortenURL(API_KEY, 'https://github.com', print)
function printShortened(err, res) {
if (err) return console.error(err)
console.log('shortened url: ', res)
// => shortened url: https://goo.gl/un5E
}
// Expand a shortened URL
expandURL(API_KEY, 'https://goo.gl/un5E', 'FULL', print)
function print(err, res) {
if (err) return console.error(err)
console.log('expanded url: ', res)
// => expanded url: https://github.com
}Installation
npm install gapi-urlTable of Contents generated with DocToc
API
shortenURL
Shortens the provided url
Parameters
apiKeyString api key of your google applicationlinkString that you want to shortencbFunction with following signaturefunction (err, shortenedLink)
expandURL
Expands the provided shortened url
Supported projections
- ANALYTICS_CLICKS: Returns only click counts
- ANALYTICS_TOP_STRINGS: Returns only top string counts
- FULL: Returns the creation timestamp and all available analytics
Parameters
apiKeyString api key of your google applicationshortenedString link that you want to expandprojectionString to include more infoANALYTICS_CLICKS| ANALYTICS_TOP_STRINGS | FULLcbFunction with following signaturefunction (err, expanedLink)shortLink
getAnalyticsURL
Returns the url for the google analytics page for the given shortened url.
Example
getAnalyticsURL('https://goo.gl/un5E')
=> https://goo.gl/#analytics/goo.gl/un5E/all_time
Parameters
urlString shortened URL, i.e. obtained via shortenURL
Returns String link to the Google Analytics page.
Resources
Getting Started with the Google Shortener API
API documented here
https://developers.google.com/url-shortener/v1/urlGet API key
https://developers.google.com/url-shortener/v1/getting_startedView Shortener Console
https://console.developers.google.com/apis/api/urlshortener.googleapis.com/overview?project=gapi-url-1513446857763&duration=PT1HTesting with curl
curl https://www.googleapis.com/urlshortener/v1/url?key=AIzaSyA2LZbzpowavq0euPXmNhrSW6Q-R4-HnZA \
-H 'Content-Type: application/json' \
-d '{"longUrl": "http://www.google.com/" }'Kudos
Thanks to @anvaka for his initial shortener implementation.
License
MIT
1.0.0
8 years ago