Skip to content

Commit b955a89

Browse files
committed
Fix correct tracking of all links
1 parent e7d4317 commit b955a89

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

src/email/email_variables.nim

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,23 @@ proc finalizeEmail(
4949
)
5050

5151

52+
template linkReplace() =
53+
## Link replacing
54+
## 1. Get all href="..." links and convert it to base64
55+
## 2. Replace the link with a `customurl/base64` string
56+
if mailUUID != "":
57+
let reLink = re("""href="([^"]+)"""")
58+
let links = findAll(result, reLink)
59+
for link in links:
60+
let
61+
base64 = encode(link.split("\"")[1])
62+
63+
const
64+
baseUrl = "/webhook/tracking/$1/click/$2"
65+
66+
result = result.replace(link, "href=\"" & hostname & baseUrl.format(mailUUID, base64) & "\"")
67+
68+
5269
proc emailVariableReplace*(contactID, message, subjectChecked: string, mailUUID = "", ignoreUnsubscribe = false): string =
5370
## Replace variables in a message.
5471
##
@@ -65,8 +82,8 @@ proc emailVariableReplace*(contactID, message, subjectChecked: string, mailUUID
6582

6683
result = message
6784

68-
let re = re("""{{\s*(\w+)\s*(?:\|\s*(\w+))?\s*}}""")
69-
let matches = findAll(message, re)
85+
let reRes = re("""{{\s*(\w+)\s*(?:\|\s*(\w+))?\s*}}""")
86+
let matches = findAll(message, reRes)
7087

7188

7289
const internalFields = @["firstname", "lastname", "name", "email", "pagename", "hostname", "uuid"]
@@ -140,30 +157,11 @@ proc emailVariableReplace*(contactID, message, subjectChecked: string, mailUUID
140157

141158

142159

143-
#
144-
# Link replacing
145-
#
146-
# 1. Get all href="..." links and convert it to base64
147-
# 2. Replace the link with a `customurl/base64` string
148-
#
149-
if mailUUID != "":
150-
let reLink = re("""href="([^"]+)"""")
151-
let links = findAll(message, reLink)
152-
for link in links:
153-
let
154-
base64 = encode(link.split("\"")[1])
155-
156-
const
157-
baseUrl = "/webhook/tracking/$1/click/$2"
158-
159-
result = result.replace(link, "href=\"" & hostname & baseUrl.format(mailUUID, base64) & "\"")
160-
161-
162-
163160
#
164161
# If there's no special attributes, we can just return the message.
165162
#
166163
if matchesChecked.len == 0:
164+
linkReplace()
167165
return finalizeEmail(
168166
result,
169167
subjectChecked,
@@ -198,6 +196,8 @@ proc emailVariableReplace*(contactID, message, subjectChecked: string, mailUUID
198196

199197
result = result.multiReplace(multi)
200198

199+
linkReplace()
200+
201201
return finalizeEmail(
202202
result,
203203
subjectChecked,

0 commit comments

Comments
 (0)