diff --git a/tracker/constants.go b/tracker/constants.go index e55b035..a99bc07 100644 --- a/tracker/constants.go +++ b/tracker/constants.go @@ -66,6 +66,8 @@ const ( IP_ADDRESS = "ip" USERAGENT = "ua" DOMAIN_UID = "duid" + DOMAIN_SID = "sid" + DOMAIN_SIDX = "vid" NETWORK_UID = "tnuid" // Page View diff --git a/tracker/subject.go b/tracker/subject.go index 4d74472..43ca93d 100644 --- a/tracker/subject.go +++ b/tracker/subject.go @@ -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" ) @@ -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]) + } + } +} diff --git a/tracker/subject_test.go b/tracker/subject_test.go index ef2944d..51cd0c2 100644 --- a/tracker/subject_test.go +++ b/tracker/subject_test.go @@ -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]) }