Skip to content

Commit

Permalink
feat: Add sendHitAnalytics when requesting
Browse files Browse the repository at this point in the history
  • Loading branch information
Chadiii committed Oct 31, 2024
1 parent 2031947 commit 27f2b95
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
12 changes: 12 additions & 0 deletions models/token.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models

import models "github.com/flagship-io/abtasty-cli/models/web_experimentation"

type MfaRequestWE struct {
MfaToken string `json:"mfa_token"`
MfaMethods []string `json:"mfa_methods"`
Expand Down Expand Up @@ -37,6 +39,16 @@ type TokenResponse struct {
Scope string `json:"scope"`
}

type UserMe struct {
Id int `json:"id,omitempty"`
Email string `json:"email"`
FirstName string `json:"firstname"`
LastName string `json:"lastname"`
Societe string `json:"societe"`
IsABTasty bool `json:"is_abtasty"`
LastAccount models.AccountWE `json:"last_account"`
}

type ClientCredentialsRequest struct {
GrantType string `json:"grant_type"`
Scope string `json:"scope,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions utils/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ func DefaultGlobalCodeWorkingDir() string {
const FEATURE_EXPERIMENTATION = "fe"
const WEB_EXPERIMENTATION = "we"
const HOME_CLI = ".cli"
const HIT_ANALYTICS_URL = "https://events.flagship.io/analytics"
92 changes: 92 additions & 0 deletions utils/http_request/common/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
)

var UserAgent string
var OutputFormat string

var c = http.Client{Timeout: time.Duration(10) * time.Second}
var counter = false
Expand All @@ -35,6 +36,8 @@ type ResourceRequest struct {
AccountID string `mapstructure:"account_id"`
AccountEnvironmentID string `mapstructure:"account_environment_id"`
WorkingDir string `mapstructure:"working_dir"`
Identifier string `mapstructure:"identifier"`
Email string `mapstructure:"email"`
}

func (c *ResourceRequest) Init(cL *RequestConfig) {
Expand Down Expand Up @@ -68,6 +71,30 @@ type RequestConfig struct {
CurrentUsedCredential string `mapstructure:"current_used_credential"`
OutputFormat string `mapstructure:"output_format"`
WorkingDirectory string `mapstructure:"working_dir"`
Identifier string `mapstructure:"identifier"`
Email string `mapstructure:"email"`
}

type HitRequest struct {
DS string `json:"ds"`
ClientID string `json:"cid"`
VisitorID string `json:"vid"`
Type string `json:"t"`
CustomVariable CustomVariableRequest `json:"cv"`
}

type CustomVariableRequest struct {
Version string `json:"version"`
Timestamp string `json:"timestamp"`
StackType string `json:"stack.type"`
UserAgent string `json:"user.agent"`
OutputFormat string `json:"output.format"`
EnvironmentId string `json:"envId"`
AccountId string `json:"accountId"`
HttpMethod string `json:"http.method"`
HttpURL string `json:"http.url"`
ABTastyProduct string `json:"abtasty.product"`
Identifier string `json:"identifier"`
}

var cred RequestConfig
Expand Down Expand Up @@ -106,6 +133,10 @@ func regenerateToken(product, configName string) {
}

func HTTPRequest[T any](method string, url string, body []byte) ([]byte, error) {
if !strings.Contains(cred.Email, "@abtasty.com") && (utils.WEB_EXPERIMENTATION == cred.Product || (utils.FEATURE_EXPERIMENTATION == cred.Product && cred.AccountEnvironmentID != "")) {
sendAnalyticHit(method, url)
}

var bodyIO io.Reader = nil
if body != nil {
bodyIO = bytes.NewBuffer(body)
Expand Down Expand Up @@ -261,3 +292,64 @@ func HTTPGetAllPagesWE[T any](resource string) ([]T, error) {
}
return results, nil
}

func sendAnalyticHit(method string, url string) (int, error) {
var bodyIO io.Reader = nil
var clientID = ""

if cred.Product == utils.FEATURE_EXPERIMENTATION {
clientID = cred.AccountEnvironmentID
}

if cred.Product == utils.WEB_EXPERIMENTATION {
clientID = cred.Identifier
}

var customVariable = CustomVariableRequest{
Version: "1",
Timestamp: time.Now().UTC().Format("2006-01-02T15:04:05.000Z"),
StackType: "Tools",
UserAgent: UserAgent,
ABTastyProduct: cred.Product,
EnvironmentId: cred.AccountEnvironmentID,
Identifier: cred.Identifier,
AccountId: cred.AccountID,
HttpMethod: method,
HttpURL: url,
OutputFormat: OutputFormat,
}

var hit = HitRequest{
DS: "APP",
ClientID: clientID,
VisitorID: cred.AccountID,
Type: "USAGE",
CustomVariable: customVariable,
}

body, err := json.Marshal(hit)
if err != nil {
log.Fatalf("error occurred: %v", err)
}

if body != nil {
bodyIO = bytes.NewBuffer(body)
}

req, err := http.NewRequest(http.MethodPost, utils.HIT_ANALYTICS_URL, bodyIO)
if err != nil {
log.Panicf("error occurred on request creation: %v", err)
}

req.Header.Add("Accept", `*/*`)
req.Header.Add("Accept-Encoding", `gzip, deflate, br`)
req.Header.Add("Content-Type", "application/json")

resp, err := c.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()

return resp.StatusCode, nil
}

0 comments on commit 27f2b95

Please sign in to comment.