Skip to content

Commit

Permalink
Conversions overhaul, more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jan 9, 2020
1 parent 237602e commit 43ef60d
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 113 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ You can view the generated [documentation here](https://godoc.org/github.com/ton
- [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)
- [x] [Conversions](https://docs.tonicpow.com/#75c837d5-3336-4d87-a686-d80c6f8938b9)

## 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
143 changes: 143 additions & 0 deletions conversions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
package tonicpow

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

// CreateConversionByGoalID will fire a conversion for a given goal id, if successful it will make a new Conversion
//
// For more information: https://docs.tonicpow.com/#caeffdd5-eaad-4fc8-ac01-8288b50e8e27
func (c *Client) CreateConversionByGoalID(goalID uint64, tncpwSession, additionalData string, delayInMinutes int64) (conversion *Conversion, err error) {

// Must have a name
if goalID == 0 {
err = fmt.Errorf("missing field: %s", fieldID)
return
}

// Must have a session guid
if len(tncpwSession) == 0 {
err = fmt.Errorf("missing field: %s", fieldVisitorSessionGUID)
return
}

// Start the post data
data := map[string]string{fieldGoalID: fmt.Sprintf("%d", goalID), fieldVisitorSessionGUID: tncpwSession, fieldAdditionalData: additionalData, fieldDelayInMinutes: fmt.Sprintf("%d", delayInMinutes)}

// Fire the request
var response string
if response, err = c.request(modelConversion, http.MethodPost, data, ""); 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), &conversion)
return
}

// CreateConversionByGoalName will fire a conversion for a given goal name, if successful it will make a new Conversion
//
// For more information: https://docs.tonicpow.com/#d19c9850-3832-45b2-b880-3ef2f3b7dc37
func (c *Client) CreateConversionByGoalName(goalName, tncpwSession, additionalData string, delayInMinutes int64) (conversion *Conversion, err error) {

// Must have a name
if len(goalName) == 0 {
err = fmt.Errorf("missing field: %s", fieldName)
return
}

// Must have a session guid
if len(tncpwSession) == 0 {
err = fmt.Errorf("missing field: %s", fieldVisitorSessionGUID)
return
}

// Start the post data
data := map[string]string{fieldName: goalName, fieldVisitorSessionGUID: tncpwSession, fieldAdditionalData: additionalData, fieldDelayInMinutes: fmt.Sprintf("%d", delayInMinutes)}

// Fire the request
var response string
if response, err = c.request(modelConversion, http.MethodPost, data, ""); 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), &conversion)
return
}

// CreateConversionByUserID will fire a conversion for a given goal and user id, if successful it will make a new Conversion
//
// For more information: https://docs.tonicpow.com/#d724f762-329e-473d-bdc4-aebc19dd9ea8
func (c *Client) CreateConversionByUserID(goalID, userID uint64, additionalData string, delayInMinutes int64) (conversion *Conversion, err error) {

// Must have a name
if goalID == 0 {
err = fmt.Errorf("missing field: %s", fieldID)
return
}

// Must have a user id
if userID == 0 {
err = fmt.Errorf("missing field: %s", fieldUserID)
return
}

// Start the post data
data := map[string]string{fieldGoalID: fmt.Sprintf("%d", goalID), fieldUserID: fmt.Sprintf("%d", userID), fieldAdditionalData: additionalData, fieldDelayInMinutes: fmt.Sprintf("%d", delayInMinutes)}

// Fire the request
var response string
if response, err = c.request(modelConversion, http.MethodPost, data, ""); 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), &conversion)
return
}

// GetConversion will get an existing conversion
// This will return an error if the goal is not found (404)
//
// For more information: https://docs.tonicpow.com/#fce465a1-d8d5-442d-be22-95169170167e
func (c *Client) GetConversion(conversionID uint64) (conversion *Conversion, err error) {

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

// Fire the request
var response string
if response, err = c.request(fmt.Sprintf("%s/details/%d", modelConversion, conversionID), 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), &conversion)
return
}
14 changes: 10 additions & 4 deletions definitions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ const (
fieldAdvertiserProfileID = "advertiser_profile_id"
fieldApiKey = "api_key"
fieldCampaignID = "campaign_id"
fieldDelayInMinutes = "delay_in_minutes"
fieldEmail = "email"
fieldGoalID = "goal_id"
fieldID = "id"
fieldLinkID = "link_id"
fieldName = "name"
Expand All @@ -26,6 +28,7 @@ const (
// Model names (used for request endpoints)
modelAdvertiser = "advertisers"
modelCampaign = "campaigns"
modelConversion = "conversions"
modelGoal = "goals"
modelLink = "links"
modelUser = "users"
Expand Down Expand Up @@ -133,15 +136,18 @@ type Goal struct {
Title string `json:"title,omitempty"`
}

// Conversion is the result of goal.Convert()
// Conversion is the response of getting a conversion
//
// For more information: https://docs.tonicpow.com/#caeffdd5-eaad-4fc8-ac01-8288b50e8e27
// For more information: https://docs.tonicpow.com/#75c837d5-3336-4d87-a686-d80c6f8938b9
type Conversion struct {
AdditionalData string `json:"additional_data,omitempty"`
ConversionTxID string `json:"conversion_tx_id,omitempty"`
Amount uint64 `json:"amount,omitempty"`
GoalID uint64 `json:"goal_id,omitempty"`
GoalName string `json:"goal_name,omitempty"`
UserID string `json:"user_id,omitempty"`
ID uint64 `json:"ID,omitempty"`
PayoutAfter string `json:"payout_after,omitempty"`
Status string `json:"status,omitempty"`
TxID string `json:"tx_id,omitempty"`
}

// Link is the link model (child of User) (relates Campaign to User)
Expand Down
42 changes: 41 additions & 1 deletion examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func main() {
CampaignID: campaign.ID,
Description: "Bring leads and get paid!",
Name: "new-lead-landing-page",
PayoutRate: 0.50,
PayoutRate: 0.02,
PayoutType: "flat",
Title: "Landing Page Leads",
}
Expand Down Expand Up @@ -295,6 +295,46 @@ func main() {
if visitorSession, err = getVisitorSession(visitorSession.TncpwSession); err != nil {
os.Exit(1)
}

log.Printf("visitor session found: %s", visitorSession.TncpwSession)

//
// Example: Fire a conversion on a goal (by user id)
//
var newConversion *tonicpow.Conversion
if newConversion, err = TonicPowAPI.CreateConversionByUserID(goal.ID, user.ID, "", 5); err != nil {
os.Exit(1)
}

log.Printf("successful conversion event: %d", newConversion.ID)

//
// Example: Fire a conversion on a goal (by visitor)
//
if newConversion, err = TonicPowAPI.CreateConversionByGoalID(goal.ID, visitorSession.TncpwSession, "", 10); err != nil {
os.Exit(1)
}

log.Printf("successful conversion event: %d payout after: %s", newConversion.ID, newConversion.PayoutAfter)

//
// Example: Get conversion
//
var conversion *tonicpow.Conversion
if conversion, err = TonicPowAPI.GetConversion(newConversion.ID); err != nil {
os.Exit(1)
}

log.Printf("got conversion: %d", conversion.ID)

/*if newConversion, err = TonicPowAPI.CreateConversionByUserID(1, 1, "", 0); err != nil {
os.Exit(1)
}*/

/*if newConversion, err = TonicPowAPI.CreateConversionByUserID(1, 1, "", 1); err != nil {
os.Exit(1)
}*/

}

//
Expand Down
108 changes: 0 additions & 108 deletions goals.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,111 +99,3 @@ func (c *Client) UpdateGoal(goal *Goal, userSessionToken string) (updatedGoal *G
err = json.Unmarshal([]byte(response), &updatedGoal)
return
}

// ConvertGoalByID will fire a conversion for a given goal id, if successful it will make a new Conversion
//
// For more information: https://docs.tonicpow.com/#caeffdd5-eaad-4fc8-ac01-8288b50e8e27
func (c *Client) ConvertGoalByID(goalID uint64, tncpwSession, additionalData string) (conversion *Conversion, err error) {

// Must have a name
if goalID == 0 {
err = fmt.Errorf("missing field: %s", fieldID)
return
}

// Must have a session guid
if len(tncpwSession) == 0 {
err = fmt.Errorf("missing field: %s", fieldVisitorSessionGUID)
return
}

// Start the post data
data := map[string]string{fieldID: fmt.Sprintf("%d", goalID), fieldVisitorSessionGUID: tncpwSession, fieldAdditionalData: additionalData}

// Fire the request
var response string
if response, err = c.request(fmt.Sprintf("%s/convert", modelGoal), http.MethodPost, data, ""); 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), &conversion)
return
}

// ConvertGoalByName will fire a conversion for a given goal name, if successful it will make a new Conversion
//
// For more information: https://docs.tonicpow.com/#d19c9850-3832-45b2-b880-3ef2f3b7dc37
func (c *Client) ConvertGoalByName(goalName, tncpwSession, additionalData string) (conversion *Conversion, err error) {

// Must have a name
if len(goalName) == 0 {
err = fmt.Errorf("missing field: %s", fieldName)
return
}

// Must have a session guid
if len(tncpwSession) == 0 {
err = fmt.Errorf("missing field: %s", fieldVisitorSessionGUID)
return
}

// Start the post data
data := map[string]string{fieldName: goalName, fieldVisitorSessionGUID: tncpwSession, fieldAdditionalData: additionalData}

// Fire the request
var response string
if response, err = c.request(fmt.Sprintf("%s/convert", modelGoal), http.MethodPost, data, ""); 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), &conversion)
return
}

// ConvertGoalByUserID will fire a conversion for a given goal and user id, if successful it will make a new Conversion
//
// For more information: https://docs.tonicpow.com/#d724f762-329e-473d-bdc4-aebc19dd9ea8
func (c *Client) ConvertGoalByUserID(goalID, userID uint64, additionalData string) (conversion *Conversion, err error) {

// Must have a name
if goalID == 0 {
err = fmt.Errorf("missing field: %s", fieldID)
return
}

// Must have a user id
if userID == 0 {
err = fmt.Errorf("missing field: %s", fieldUserID)
return
}

// Start the post data
data := map[string]string{fieldID: fmt.Sprintf("%d", goalID), fieldUserID: fmt.Sprintf("%d", userID), fieldAdditionalData: additionalData}

// Fire the request
var response string
if response, err = c.request(fmt.Sprintf("%s/convert", modelGoal), http.MethodPost, data, ""); 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), &conversion)
return
}

0 comments on commit 43ef60d

Please sign in to comment.