Last active
February 18, 2021 08:54
-
-
Save u0401006/cb38df1c8f376f4667cdc6d2253283b1 to your computer and use it in GitHub Desktop.
google app script custom formula bitly()
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
function bitly(source,date,content,who,url) { | |
var d= (new Date(date.getTime()+3600000*24)).toISOString().slice(0,10).replace(/20/, "").replace("-","").replace("-",""); | |
//宣告一個變數叫d,取輸入的日期來清整格式 | |
var utm2= content.substring(0, 10)+'-'; | |
//避免網址太長或者是遇到特殊字被截斷,活動名稱只取10個字 | |
if (who.match("A|B")){ | |
var utm3= 'marketing-'+utm2; | |
} else if (who.match("C|D")) { | |
var utm3= 'ec-'+utm2; | |
} else { | |
var utm3= 'ad-'+utm2; | |
} | |
//根據需求者加上部門代號 | |
if (url.indexOf("?")!=-1){ | |
var url1 = url+'&'; | |
} else{ | |
var url1 = url+'?'; | |
} | |
//因爲加utm有其規則,網址中沒有?的話,要以?開始接utm與參數 | |
if (url.match("books.com.tw")) { | |
var utm1 = url1+'utm_source= #博客來提供的utm# '; | |
} else if (source=="大FB"){ | |
var utm1 = url1+'utm_source=BIG.FB&utm_medium=social&utm_campaign=cp-f1-'+utm3+d; | |
} else if (source=="小FB") { | |
var utm1 = url1+'utm_source=SMALL.FB&utm_medium=social&utm_campaign=cp-f2-'+utm3+d; | |
} else if (source=="大Line") { | |
var utm1 = url1+'utm_source=BIG.Line&utm_medium=social&utm_campaign=cp-l1-'+utm3+d; | |
} else if (source=="小Line") { | |
var utm1 = url1+'utm_source=SMALL.Line&utm_medium=social&utm_campaign=cp-l2-'+utm3+d; | |
} | |
//判斷發出管道,加入相應的來源 | |
var uri = encodeURIComponent(utm1.toString()); | |
//串接bitly api,要以uri形式傳送,因此要把url轉碼 | |
var link = "https://api-ssl.bitly.com/v3/shorten?format=txt&access_token= #你的bitly token# &longUrl=".concat(uri); | |
//以這個網址向bitly主機發request | |
var response = (UrlFetchApp.fetch(link).getContentText("UTF-8")).replace("\n",""); | |
return response; | |
//宣告一個變數response來承接bitly伺服器回傳的短網址。並且在原儲存格顯示短網址 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment