Skip to content

Commit

Permalink
Visitor session support
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jan 4, 2020
1 parent fdd9b88 commit 36d9347
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ You can view the generated [documentation here](https://godoc.org/github.com/ton
- [x] [Campaigns](https://docs.tonicpow.com/#5aca2fc7-b3c8-445b-aa88-f62a681f8e0c)
- [x] [Goals](https://docs.tonicpow.com/#316b77ab-4900-4f3d-96a7-e67c00af10ca)
- [x] [Links](https://docs.tonicpow.com/#ee74c3ce-b4df-4d57-abf2-ccf3a80e4e1e)
- [x] [Visitors](https://docs.tonicpow.com/#d0d9055a-0c92-4f55-a370-762d44acf801)

## Examples & Tests
All unit tests and [examples](tonicpow_test.go) run via [Travis CI](https://travis-ci.org/tonicpow/go-tonicpow) and uses [Go version 1.13.x](https://golang.org/doc/go1.13). View the [deployment configuration file](.travis.yml).
Expand Down
23 changes: 18 additions & 5 deletions definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
fieldCampaignID = "campaign_id"
fieldEmail = "email"
fieldID = "id"
fieldLinkID = "link_id"
fieldName = "name"
fieldPassword = "password"
fieldPasswordConfirm = "password_confirm"
Expand All @@ -23,11 +24,13 @@ const (
fieldVisitorSessionGUID = "tncpw_session"

// Model names (used for request endpoints)
modelAdvertiser = "advertisers"
modelCampaign = "campaigns"
modelGoal = "goals"
modelLink = "links"
modelUser = "users"
modelAdvertiser = "advertisers"
modelCampaign = "campaigns"
modelGoal = "goals"
modelLink = "links"
modelUser = "users"
modelVisitors = "visitors"
modelVisitorSession = "sessions"

// apiVersion current version for all endpoints
apiVersion = "v1"
Expand Down Expand Up @@ -153,3 +156,13 @@ type Link struct {
ShortCodeURL string `json:"short_code_url,omitempty"`
UserID uint64 `json:"user_id,omitempty"`
}

// VisitorSession is the session for any visitor or user (related to link and campaign)
//
// For more information: https://docs.tonicpow.com/#d0d9055a-0c92-4f55-a370-762d44acf801
type VisitorSession struct {
AdditionalData string `json:"additional_data,omitempty"`
CampaignID uint64 `json:"campaign_id,omitempty"`
LinkID uint64 `json:"link_id,omitempty"`
TncpwSession string `json:"tncpw_session,omitempty"`
}
24 changes: 23 additions & 1 deletion examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func main() {
if link, err = TonicPowAPI.GetLink(link.ID, userSessionToken); err != nil {
log.Fatalf("get link failed - api error: %s", TonicPowAPI.LastRequest.Error.Message)
} else {
log.Printf("got link by id %d", goal.ID)
log.Printf("got link by id %d", link.ID)
}

//
Expand All @@ -297,4 +297,26 @@ func main() {
//if err = TonicPowAPI.ActivateUser(user.ID); err != nil {
// log.Fatalf("activate user failed - api error: %s", TonicPowAPI.LastRequest.Error.Message)
//}

//
// Example: Create a Visitor Session
//
visitorSession := &tonicpow.VisitorSession{
AdditionalData: "my custom data",
LinkID: link.ID,
}
if visitorSession, err = TonicPowAPI.CreateVisitorSession(visitorSession); err != nil {
log.Fatalf("create visitor session failed - api error: %s data: %s", TonicPowAPI.LastRequest.Error.Message, TonicPowAPI.LastRequest.Error.Data)
} else {
log.Printf("visitor session %s created", visitorSession.TncpwSession)
}

//
// Example: Get Visitor Session
//
if visitorSession, err = TonicPowAPI.GetVisitorSession(visitorSession.TncpwSession); err != nil {
log.Fatalf("get visitor session failed - api error: %s", TonicPowAPI.LastRequest.Error.Message)
} else {
log.Printf("got visitor session by %s", visitorSession.TncpwSession)
}
}
62 changes: 62 additions & 0 deletions visitors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package tonicpow

import (
"encoding/json"
"fmt"
"net/http"
)

// CreateVisitorSession will make a new session for a visitor (used for goal conversions)
//
// For more information: https://docs.tonicpow.com/#29a93e9b-9726-474c-b25e-92586200a803
func (c *Client) CreateVisitorSession(visitorSession *VisitorSession) (createdSession *VisitorSession, err error) {

// Basic requirements
if visitorSession.LinkID == 0 {
err = fmt.Errorf("missing required attribute: %s", fieldLinkID)
return
}

// Fire the request
var response string
if response, err = c.request(fmt.Sprintf("%s/%s", modelVisitors, modelVisitorSession), http.MethodPost, visitorSession, ""); err != nil {
return
}

// Only a 201 is treated as a success
if err = c.error(http.StatusCreated, response); err != nil {
return
}

// Convert model response
err = json.Unmarshal([]byte(response), &createdSession)
return
}

// GetVisitorSession will get a visitor session
// This will return an error if the session is not found (404)
//
// For more information: https://docs.tonicpow.com/#cf560448-6dda-42a6-9051-136afabe78e6
func (c *Client) GetVisitorSession(visitorSessionGUID string) (visitorSession *VisitorSession, err error) {

// Must have an id
if len(visitorSessionGUID) == 0 {
err = fmt.Errorf("missing field: %s", fieldVisitorSessionGUID)
return
}

// Fire the request
var response string
if response, err = c.request(fmt.Sprintf("%s/%s/details/%s", modelVisitors, modelVisitorSession, visitorSessionGUID), http.MethodGet, nil, ""); err != nil {
return
}

// Only a 200 is treated as a success
if err = c.error(http.StatusOK, response); err != nil {
return
}

// Convert model response
err = json.Unmarshal([]byte(response), &visitorSession)
return
}

0 comments on commit 36d9347

Please sign in to comment.