Skip to content

Commit

Permalink
Merge pull request #13 from tonicpow/feature/convert-by-twitter-id
Browse files Browse the repository at this point in the history
convert by twitter id
  • Loading branch information
mrz1836 committed Jan 24, 2022
2 parents a5858c9 + 678e184 commit 8e86090
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
16 changes: 13 additions & 3 deletions conversions.go
Expand Up @@ -20,6 +20,7 @@ type conversionOptions struct {
shortCode string // (optional) trigger a conversion for a link short_code
tncpwSession string // tncpw session
tonicPowUserID uint64 // (optional) trigger a conversion for a specific user
twitterID uint64 // (optional) trigger a conversion for a specific twitter user
}

// validate will check the options before processing
Expand All @@ -28,10 +29,10 @@ func (o *conversionOptions) validate() error {
return fmt.Errorf("missing required attribute(s): %s or %s", fieldID, fieldName)
} else if o.goalID == 0 && o.tonicPowUserID > 0 {
return fmt.Errorf("missing required attribute: %s", fieldID)
} else if o.tonicPowUserID == 0 && len(o.tncpwSession) == 0 && len(o.shortCode) == 0 {
} else if o.tonicPowUserID == 0 && len(o.tncpwSession) == 0 && len(o.shortCode) == 0 && o.twitterID == 0 {
return fmt.Errorf(
"missing required attribute(s): %s or %s or %s",
fieldVisitorSessionGUID, fieldUserID, fieldShortCode,
"missing required attribute(s): %s or %s or %s or %s",
fieldVisitorSessionGUID, fieldUserID, fieldShortCode, fieldTwitterID,
)
}
return nil
Expand All @@ -56,6 +57,8 @@ func (o *conversionOptions) payload() map[string]string {
m[fieldUserID] = fmt.Sprintf("%d", o.tonicPowUserID)
} else if len(o.shortCode) > 0 {
m[fieldShortCode] = o.shortCode
} else if o.twitterID > 0 {
m[fieldTwitterID] = fmt.Sprintf("%d", o.twitterID)
} else if len(o.tncpwSession) > 0 {
m[fieldVisitorSessionGUID] = o.tncpwSession
}
Expand Down Expand Up @@ -134,6 +137,13 @@ func WithShortCode(shortCode string) ConversionOps {
}
}

// WithTwitterID will set a twitter user ID
func WithTwitterID(twitterID uint64) ConversionOps {
return func(c *conversionOptions) {
c.twitterID = twitterID
}
}

// CreateConversion 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
Expand Down
26 changes: 26 additions & 0 deletions conversions_test.go
Expand Up @@ -156,6 +156,32 @@ func TestClient_CreateConversion(t *testing.T) {
assert.Equal(t, testGoalID, newConversion.GoalID)
})

t.Run("create a conversion by goal id / twitter ID (success)", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
assert.NotNil(t, client)

conversion := newTestConversion()

endpoint := fmt.Sprintf("%s/%s", EnvironmentDevelopment.apiURL, modelConversion)

err = mockResponseData(http.MethodPost, endpoint, http.StatusCreated, conversion)
assert.NoError(t, err)

var newConversion *Conversion
var response *StandardResponse
newConversion, response, err = client.CreateConversion(
WithGoalID(testGoalID),
WithTwitterID(testTwitterID),
)

assert.NoError(t, err)
assert.NotNil(t, newConversion)
assert.NotNil(t, response)
assert.Equal(t, testConversionID, newConversion.ID)
assert.Equal(t, testGoalID, newConversion.GoalID)
})

t.Run("missing goal id and session/user_id", func(t *testing.T) {
client, err := newTestClient()
assert.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions definitions.go
Expand Up @@ -37,6 +37,7 @@ const (
fieldSortOrder = "sort_order"
fieldTargetURL = "target_url"
fieldTitle = "title"
fieldTwitterID = "twitter_id"
fieldUserID = "user_id"
fieldVisitorSessionGUID = "tncpw_session"

Expand Down
1 change: 1 addition & 0 deletions tonicpow_test.go
Expand Up @@ -23,6 +23,7 @@ const (
testRateCurrency string = "usd"
testShortCode string = "test_short_code"
testTncpwSession string = "TestSessionKey12345678987654321"
testTwitterID uint64 = 22413277
testUserID uint64 = 43
)

Expand Down

0 comments on commit 8e86090

Please sign in to comment.