Skip to content

Commit

Permalink
Changed method signatures for converting goals
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Jan 4, 2020
1 parent 3377077 commit 64ae843
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 21 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ import (

func main() {
api, _ := tonicpow.NewClient(os.Getenv("TONICPOW_API_KEY"), tonicpow.LiveEnvironment, nil)
_ = api.ConvertGoalWithSession(123, "visitor-tncpw-session-guid", "from: my blog")
_, _ = api.ConvertGoalByGoalName("landing-page-lead", "visitor-tncpw-session-guid", "from: my blog")
}
```

Expand Down
42 changes: 26 additions & 16 deletions examples/examples.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,19 +127,15 @@ func main() {
HomepageURL: "https://tonicpow.com",
IconURL: "https://tonicpow.com/images/logos/apple-touch-icon.png",
}
if advertiser, err = TonicPowAPI.CreateAdvertiserProfile(advertiser, ""); err != nil {
log.Fatalf("create advertiser failed - api error: %s data: %s", TonicPowAPI.LastRequest.Error.Message, TonicPowAPI.LastRequest.Error.Data)
} else {
log.Printf("advertiser profile %s id %d created", advertiser.Name, advertiser.ID)
if advertiser, err = createAdvertiserProfile(advertiser, ""); err != nil {
os.Exit(1)
}

//
// Example: Get an advertiser profile
//
if advertiser, err = TonicPowAPI.GetAdvertiserProfile(advertiser.ID, ""); err != nil {
log.Fatalf("get advertiser profile failed - api error: %s", TonicPowAPI.LastRequest.Error.Message)
} else {
log.Printf("got advertiser profile by id %d", advertiser.ID)
if advertiser, err = getAdvertiserProfile(advertiser.ID, ""); err != nil {
os.Exit(1)
}

//
Expand Down Expand Up @@ -180,19 +176,15 @@ func main() {
HomepageURL: "https://tonicpow.com",
IconURL: "https://tonicpow.com/images/logos/apple-touch-icon.png",
}
if advertiser, err = TonicPowAPI.CreateAdvertiserProfile(advertiser, userSessionToken); err != nil {
log.Fatalf("create advertiser failed - api error: %s data: %s", TonicPowAPI.LastRequest.Error.Message, TonicPowAPI.LastRequest.Error.Data)
} else {
log.Printf("advertiser profile %s id %d created", advertiser.Name, advertiser.ID)
if advertiser, err = createAdvertiserProfile(advertiser, userSessionToken); err != nil {
os.Exit(1)
}

//
// Example: Get Advertiser Profile
//
if advertiser, err = TonicPowAPI.GetAdvertiserProfile(advertiser.ID, userSessionToken); err != nil {
log.Fatalf("get advertiser profile failed - api error: %s", TonicPowAPI.LastRequest.Error.Message)
} else {
log.Printf("got advertiser profile by id %d", advertiser.ID)
if advertiser, err = getAdvertiserProfile(advertiser.ID, userSessionToken); err != nil {
os.Exit(1)
}

//
Expand Down Expand Up @@ -309,6 +301,24 @@ func main() {
// Example Functions
//

func createAdvertiserProfile(profile *tonicpow.AdvertiserProfile, userSessionToken string) (createdProfile *tonicpow.AdvertiserProfile, err error) {
if createdProfile, err = TonicPowAPI.CreateAdvertiserProfile(profile, userSessionToken); err != nil {
log.Fatalf("create advertiser failed - api error: %s data: %s", TonicPowAPI.LastRequest.Error.Message, TonicPowAPI.LastRequest.Error.Data)
} else {
log.Printf("advertiser profile %s id %d created", createdProfile.Name, createdProfile.ID)
}
return
}

func getAdvertiserProfile(advertiserProfileID uint64, userSessionToken string) (advertiserProfile *tonicpow.AdvertiserProfile, err error) {
if advertiserProfile, err = TonicPowAPI.GetAdvertiserProfile(advertiserProfileID, userSessionToken); err != nil {
log.Fatalf("get advertiser profile failed - api error: %s", TonicPowAPI.LastRequest.Error.Message)
} else {
log.Printf("got advertiser profile by id %d", advertiserProfile.ID)
}
return
}

func createCampaign(campaign *tonicpow.Campaign, userSessionToken string) (createdCampaign *tonicpow.Campaign, err error) {
if createdCampaign, err = TonicPowAPI.CreateCampaign(campaign, userSessionToken); err != nil {
log.Fatalf("create campaign failed - api error: %s data: %s", TonicPowAPI.LastRequest.Error.Message, TonicPowAPI.LastRequest.Error.Data)
Expand Down
44 changes: 40 additions & 4 deletions goals.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,10 @@ func (c *Client) UpdateGoal(goal *Goal, userSessionToken string) (updatedGoal *G
return
}

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

// Must have a name
if goalID == 0 {
Expand Down Expand Up @@ -136,10 +136,46 @@ func (c *Client) ConvertGoalWithVisitorSession(goalID uint64, tncpwSession, addi
return
}

// ConvertGoalWithUserID will fire a conversion for a given goal, if successful it will make a new Conversion
// ConvertGoalByGoalID will fire a conversion for a given goal, if successful it will make a new Conversion
//
// For more information: https://docs.tonicpow.com/#d19c9850-3832-45b2-b880-3ef2f3b7dc37
func (c *Client) ConvertGoalByGoalName(goalName string, 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, if successful it will make a new Conversion
//
// For more information: https://docs.tonicpow.com/#d724f762-329e-473d-bdc4-aebc19dd9ea8
func (c *Client) ConvertGoalWithUserID(goalID uint64, userID uint64, additionalData string) (conversion *Conversion, err error) {
func (c *Client) ConvertGoalByUserID(goalID uint64, userID uint64, additionalData string) (conversion *Conversion, err error) {

// Must have a name
if goalID == 0 {
Expand Down

0 comments on commit 64ae843

Please sign in to comment.