Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting Subject session information (closes #78) #79

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice touch!

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])
}
Loading