-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optionally track analytics for hardware versions of providers (#5)
* Optionally track analytics for hardware versions of providers * Run pre-commit
- Loading branch information
1 parent
4481f07
commit c00b355
Showing
5 changed files
with
99 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.idea | ||
|
||
/registration_relay |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package analytics | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
var ConfigURL = "" | ||
var ConfigToken = "" | ||
var client http.Client | ||
|
||
var logger = log.With().Str("component", "analytics").Logger() | ||
|
||
func trackImplementation(userId string, event string, properties map[string]any) { | ||
var buf bytes.Buffer | ||
err := json.NewEncoder(&buf).Encode(map[string]interface{}{ | ||
"userId": userId, | ||
"event": event, | ||
"properties": properties, | ||
}) | ||
if err != nil { | ||
logger.Error().Err(err).Msg("error encoding payload") | ||
return | ||
} | ||
|
||
req, err := http.NewRequest(http.MethodPost, ConfigURL, &buf) | ||
if err != nil { | ||
logger.Error().Err(err).Msg("error creating request") | ||
return | ||
} | ||
req.SetBasicAuth(ConfigToken, "") | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
logger.Error().Err(err).Msg("error sending request") | ||
return | ||
} | ||
err = resp.Body.Close() | ||
if err != nil { | ||
logger.Error().Err(err).Msg("error closing request") | ||
} | ||
|
||
logger.Info().Str("event", event).Msg("Tracked event") | ||
} | ||
|
||
func IsEnabled() bool { | ||
return len(ConfigToken) > 0 | ||
} | ||
|
||
func Track(userId string, event string, properties map[string]any) { | ||
if !IsEnabled() { | ||
return | ||
} | ||
|
||
go trackImplementation(userId, event, properties) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters