Last active
November 1, 2017 19:35
-
-
Save cspenn/b4b100ccc989890d3865e20f260b399a to your computer and use it in GitHub Desktop.
Makes bit.ly links with your designated UTM attribution codes!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/*********************************************** | |
Bitly Google Analytics Preparation Script | |
Copyright © 2016-2017 Christopher S. Penn | |
The purpose of this code is to transform a list of plain text URLs and turn them into bit.ly links. | |
Requirements: | |
A machine with PHP 5+ on it | |
cUrl for PHP/CLI | |
A bit.ly API key | |
Read/write access to the local disk | |
Instructions: | |
Run this file locally; the arguments should be the input file, followed by source, medium, and campaign, with no spaces or punctuation other than dashes | |
Example: | |
php makebitlylinks.php mylinklist.txt facebook social-media cspenn | |
License: | |
This program is free software: you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation, either version 3 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program. If not, see <http://www.gnu.org/licenses/>. | |
************************************************/ | |
// configuration stuff - change the timezone if you want it to be accurate for your machine | |
date_default_timezone_set('America/New_York'); | |
ini_set('auto_detect_line_endings', TRUE); | |
// Your bit.ly API key goes here! | |
$apikey = "YOURBITLYAPIKEYGOESHERE"; | |
// UTM tracking codes are passed in as parameters | |
$utmsource = $argv[2]; // source, like Facebook | |
$utmmedium = $argv[3]; // medium, like social or PPC | |
$utmcampaign = $argv[4]; // campaign, like may-2017-posts | |
// some defaults you can fill in if you forget to specify them | |
if ($utmsource == "") { | |
$utmsource = "Facebook"; | |
} | |
if ($utmmedium == "") { | |
$utmmedium = "socialmedia"; | |
} | |
if ($utmcampaign == "") { | |
$utmcampaign = "cspenn"; | |
} | |
/************************************** | |
Nothing else to change for the user below this point | |
***************************************/ | |
// logfile outputs as a txt file | |
$stamp = date("Y-m-d-h-i-s"); | |
$shortstamp = date("Y-m-d"); | |
$logfile = "bitlylinks-$stamp.txt"; | |
$fp = fopen($logfile, "w"); | |
// input file to open | |
$handle = @fopen($argv[1], "r"); | |
// parse the file, line by line | |
if ($handle) { | |
while (($buffer = fgets($handle, 4096)) !== false) { | |
$longurl = trim($buffer); | |
// break off existing parameters - warning, this will break standard YouTube links! | |
$cleanurl = strtok($longurl, '?'); | |
// add UTM codes | |
$encodeurl = $cleanurl . "?utm_source=" . $utmsource . "&utm_medium=" . $utmmedium . "&utm_campaign=" . $utmcampaign; | |
$finalurl = urlencode($encodeurl); | |
// re-encode | |
$bitly = "https://api-ssl.bitly.com/v3/shorten?&access_token=$apikey&longUrl=$finalurl"; | |
$results = json_decode(file_get_contents($bitly), true); | |
var_dump($results); | |
$theurl = $results['data']['url']; | |
fwrite($fp, "$theurl\n"); | |
} | |
if (!feof($handle)) { | |
echo "Error: unexpected fgets() fail\n"; | |
} | |
fclose($handle); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment