diff --git a/README.md b/README.md index 65f5d92..de3ab6b 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/definitions.go b/definitions.go index e0d7735..26c7a92 100644 --- a/definitions.go +++ b/definitions.go @@ -12,6 +12,7 @@ const ( fieldCampaignID = "campaign_id" fieldEmail = "email" fieldID = "id" + fieldLinkID = "link_id" fieldName = "name" fieldPassword = "password" fieldPasswordConfirm = "password_confirm" @@ -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" @@ -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"` +} diff --git a/examples/examples.go b/examples/examples.go index d322b53..45d4667 100644 --- a/examples/examples.go +++ b/examples/examples.go @@ -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) } // @@ -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) + } } diff --git a/visitors.go b/visitors.go new file mode 100644 index 0000000..d58d462 --- /dev/null +++ b/visitors.go @@ -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 +}