Files
memory-infrastructure-palace/docs/projects/memorypalace/Apple Notes/md 1.md

48 lines
3.4 KiB
Markdown

<span style="font-family:Courier;color:#000ff;">Author: Greg R. Jacobs</span>
<span style="font-family:Courier;color:#000ff;">Date: Feb 07, 2015</span>
<span style="font-family:Courier;color:#000ff;">Description: Wanted to have a Google Sheet with 3 columns.</span>
<span style="font-family:Courier;color:#000ff;">First was long URL</span>
<span style="font-family:Courier;color:#000ff;">Second needed to dynamically shorten</span>
<span style="font-family:Courier;color:#000ff;">using Goo.gl and an API key obtained from:</span>
<span style="font-family:Courier;color:#000ff;">https://code.google.com/apis/console</span>
<span style="font-family:Courier;color:#000ff;">Third to update with clicks on shortened url</span>
<span style="font-family:Courier;color:#000ff;">(this instance is referencing allTime clicks on the short URL).</span>
<span style="font-family:Courier;color:#000ff;">*/</span>
<span style="font-family:Courier;color:#000ff;">function shortenURL(longUrl,key) {</span>
<span style="font-family:Courier;color:#000ff;">var serviceUrl="https://www.googleapis.com/urlshortener/v1/url?key="+key;</span>
<span style="font-family:Courier;color:#000ff;">var options={</span>
<span style="font-family:Courier;color:#000ff;">muteHttpExceptions:true,</span>
<span style="font-family:Courier;color:#000ff;">method:"post",</span>
<span style="font-family:Courier;color:#000ff;">contentType: "application/json",</span>
<span style="font-family:Courier;color:#000ff;">payload : Utilities.jsonStringify({'longUrl': longUrl })</span>
<span style="font-family:Courier;color:#000ff;">};</span>
<span style="font-family:Courier;color:#000ff;">var response=UrlFetchApp.fetch(serviceUrl, options);</span>
<span style="font-family:Courier;color:#000ff;">if(response.getResponseCode() == 200) {</span>
<span style="font-family:Courier;color:#000ff;">var content = Utilities.jsonParse(response.getContentText());</span>
<span style="font-family:Courier;color:#000ff;">if ( (content != null) && (content["id"] != null) )</span>
<span style="font-family:Courier;color:#000ff;">return content["id"];</span>
<span style="font-family:Courier;color:#000ff;">}</span>
<span style="font-family:Courier;color:#000ff;">return longUrl;</span>
<span style="font-family:Courier;color:#000ff;">}</span>
<span style="font-family:Courier;color:#000ff;">function GetShortUrlClicks(shortURL,key) {</span>
<span style="font-family:Courier;color:#000ff;">var url = "https://www.googleapis.com/urlshortener/v1/url?key="+key+"&shortUrl="+shortURL+"&projection=ANALYTICS_CLICKS";</span>
<span style="font-family:Courier;color:#000ff;">if(shortURL != "")</span>
<span style="font-family:Courier;color:#000ff;">{</span>
<span style="font-family:Courier;color:#000ff;">var response = UrlFetchApp.fetch(url);</span>
<span style="font-family:Courier;color:#000ff;">var jsonResponse = response.toString();</span>
<span style="font-family:Courier;color:#000ff;">var data = JSON.parse(jsonResponse);</span>
<span style="font-family:Courier;color:#000ff;">Logger.log(data.analytics.allTime.shortUrlClicks);</span>
<span style="font-family:Courier;color:#000ff;">return data.analytics.allTime.shortUrlClicks;</span>
<span style="font-family:Courier;color:#000ff;">}</span>
<span style="font-family:Courier;color:#000ff;">else{}</span>
<span style="font-family:Courier;color:#000ff;">}</span>