Skip to content

Commit

Permalink
Allow setting Subject session information (closes #78)
Browse files Browse the repository at this point in the history
PR #79
  • Loading branch information
jethron authored Aug 13, 2024
1 parent 88ca5cf commit 0917f9e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tracker/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ const (
IP_ADDRESS = "ip"
USERAGENT = "ua"
DOMAIN_UID = "duid"
DOMAIN_SID = "sid"
DOMAIN_SIDX = "vid"
NETWORK_UID = "tnuid"

// Page View
Expand Down
30 changes: 30 additions & 0 deletions tracker/subject.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package tracker

import (
"strconv"
"strings"

"github.com/snowplow/snowplow-golang-tracker/v3/pkg/common"
"github.com/snowplow/snowplow-golang-tracker/v3/pkg/payload"
)
Expand Down Expand Up @@ -77,7 +80,34 @@ func (s Subject) SetDomainUserId(domainUserId string) {
s.payload.Add(DOMAIN_UID, common.NewString(domainUserId))
}

// SetDomainSessionId adds a domain session id to the key-value store.
func (s Subject) SetDomainSessionId(domainSessionId string) {
s.payload.Add(DOMAIN_SID, common.NewString(domainSessionId))
}

// SetDomainSessionIndex adds a domain user id to the key-value store.
func (s Subject) SetDomainSessionIndex(domainSessionIndex int) {
s.payload.Add(DOMAIN_SIDX, common.NewString(common.IntToString(domainSessionIndex)))
}

// SetNetworkUserId adds a network user id to the key-value store.
func (s Subject) SetNetworkUserId(networkUserId string) {
s.payload.Add(NETWORK_UID, common.NewString(networkUserId))
}

// FromIdCookie sets subject fields from the ID cookie to the key-value store.
func (s Subject) FromIdCookie(idCookie string) {
cookieParts := strings.Split(idCookie, ".")

if len(cookieParts) >= 6 {
if len(cookieParts[0]) == 36 {
s.SetDomainUserId(cookieParts[0])
}
if idx, err := strconv.Atoi(cookieParts[2]); err == nil {
s.SetDomainSessionIndex(idx)
}
if len(cookieParts[5]) == 36 {
s.SetDomainSessionId(cookieParts[5])
}
}
}
7 changes: 7 additions & 0 deletions tracker/subject_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ func TestSubjectSetFunctions(t *testing.T) {
assert.Equal("useragent-string", subjectMap[USERAGENT])
assert.Equal("domain-user-id", subjectMap[DOMAIN_UID])
assert.Equal("network-user-id", subjectMap[NETWORK_UID])

// cookie generated by JS tracker v3
subject.FromIdCookie("f398cb23-ec25-49cb-a30a-db942b915c2e.1662678537.839.1722486147.1722477600.7cd91831-3ed1-49dc-be81-5ab7fd2b01c3.3417ccdf-6858-4ef9-bd09-09b21e1e6ca2.46bdb481-6fc8-48c0-81bf-0d3ef64b1f0a.1722484481043.30")
subjectMap = subject.Get()
assert.Equal("f398cb23-ec25-49cb-a30a-db942b915c2e", subjectMap[DOMAIN_UID])
assert.Equal("839", subjectMap[DOMAIN_SIDX])
assert.Equal("7cd91831-3ed1-49dc-be81-5ab7fd2b01c3", subjectMap[DOMAIN_SID])
}

0 comments on commit 0917f9e

Please sign in to comment.