Skip to content

Commit

Permalink
Clear UTM parameters from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
RadhiFadlillah committed Mar 5, 2018
1 parent 8ebe093 commit d167379
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
26 changes: 26 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"html/template"
nurl "net/url"
"strings"
"time"

Expand Down Expand Up @@ -59,6 +60,12 @@ func addBookmark(base model.Bookmark, offline bool) (book model.Bookmark, err er
// Prepare initial result
book = base

// Clear UTM parameters from URL
book.URL, err = clearUTMParams(book.URL)
if err != nil {
return book, err
}

// Fetch data from internet
if !offline {
article, err := readability.Parse(book.URL, 10*time.Second)
Expand Down Expand Up @@ -94,3 +101,22 @@ func addBookmark(base model.Bookmark, offline bool) (book model.Bookmark, err er
func normalizeSpace(str string) string {
return strings.Join(strings.Fields(str), " ")
}

func clearUTMParams(uri string) (string, error) {
tempURL, err := nurl.Parse(uri)
if err != nil {
return "", err
}

newQuery := nurl.Values{}
for key, value := range tempURL.Query() {
if strings.HasPrefix(key, "utm_") {
continue
}

newQuery[key] = value
}

tempURL.RawQuery = newQuery.Encode()
return tempURL.String(), nil
}
7 changes: 7 additions & 0 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ func init() {

func updateBookmarks(indices []string, url, title, excerpt string, tags []string, offline bool) ([]model.Bookmark, error) {
mutex := sync.Mutex{}

// Clear UTM parameters from URL
url, err := clearUTMParams(url)
if err != nil {
return []model.Bookmark{}, err
}

// Read bookmarks from database
bookmarks, err := DB.GetBookmarks(true, indices...)
if err != nil {
Expand Down

0 comments on commit d167379

Please sign in to comment.