Skip to content

Commit

Permalink
Add new Website.GenerateAnalytics() method + deprecate all other me…
Browse files Browse the repository at this point in the history
…thods
  • Loading branch information
valeriansaliou committed Aug 27, 2024
1 parent ea8e32a commit 3cbb153
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 124 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ The Crisp API Golang wrapper. Authenticate, send messages, fetch conversations,

Copyright 2023 Crisp IM SAS. See LICENSE for copying information.

* **📝 Implements**: [REST API Reference (V1)](https://docs.crisp.chat/references/rest-api/v1/) at revision: 12/07/2024
* **📝 Implements**: [REST API Reference (V1)](https://docs.crisp.chat/references/rest-api/v1/) at revision: 27/08/2024
* **😘 Maintainer**: [@valeriansaliou](https://github.com/valeriansaliou)

## Usage
Expand Down Expand Up @@ -357,12 +357,8 @@ Refer directly to [the library source code](https://github.com/crisp-im/go-crisp
* `client.Website.ScheduleReminderForConversation(websiteID string, sessionID string, date string, note string) (*Response, error)`

* #### **Website Analytics**
* **Acquire Analytics Points** [`user`]: [Reference](https://docs.crisp.chat/references/rest-api/v1/#acquire-analytics-points)
* `client.Website.AcquireAnalyticsPoints(websiteID string, pointType string, pointMetric string, dateFrom time.Time, dateTo time.Time, dateSplit string, classifier string, filterPrimary string, filterSecondary string, filterTertiary string) (*WebsiteAnalyticsPoints, *Response, error)`
* **List Analytics Filters** [`user`]: [Reference](https://docs.crisp.chat/references/rest-api/v1/#list-analytics-filters)
* `client.Website.ListAnalyticsFilters(websiteID string, pageNumber uint, pointType string, pointMetric string, dateFrom time.Time, dateTo time.Time) (*[]WebsiteAnalyticsFilter, *Response, error)`
* **List Analytics Classifiers** [`user`]: [Reference](https://docs.crisp.chat/references/rest-api/v1/#list-analytics-classifiers)
* `client.Website.ListAnalyticsClassifiers(websiteID string, pageNumber uint, pointType string, pointMetric string, dateFrom time.Time, dateTo time.Time) (*[]WebsiteAnalyticsClassifier, *Response, error)`
* **Generate Analytics** [`user`]: [Reference](https://docs.crisp.chat/references/rest-api/v1/#generate-analytics)
* `client.Website.GenerateAnalytics(websiteID string, query AnalyticsGenerateQuery) (*[]WebsiteAnalyticsGeneratePoint, *Response, error)`

* #### **Website People**
* **Get People Statistics** [`user`, `plugin`]: [Reference](https://docs.crisp.chat/references/rest-api/v1/#get-people-statistics)
Expand Down
166 changes: 49 additions & 117 deletions crisp/website_analytics.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2018 Crisp IM SAS All rights reserved.
// Copyright 2024 Crisp IM SAS All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
Expand All @@ -8,147 +8,79 @@ package crisp

import (
"fmt"
"time"
"net/url"
)


// WebsiteAnalyticsPointsData mapping
type WebsiteAnalyticsPointsData struct {
Data *WebsiteAnalyticsPoints `json:"data,omitempty"`
// AnalyticsGenerateQuery mapping
type AnalyticsGenerateQuery struct {
Metric string `json:"metric"`
Type string `json:"type"`
Aggregator *string `json:"aggregator,omitempty"`
SplitBy *string `json:"split_by,omitempty"`
Filter *string `json:"filter,omitempty"`
Date AnalyticsGenerateQueryDate `json:"date"`
RawExport *bool `json:"raw_export,omitempty"`
}

// WebsiteAnalyticsPoints mapping
type WebsiteAnalyticsPoints struct {
Pipeline *WebsiteAnalyticsPointsPipeline `json:"pipeline,omitempty"`
Points *[]WebsiteAnalyticsPointsPoint `json:"points,omitempty"`
// AnalyticsGenerateQueryDate mapping
type AnalyticsGenerateQueryDate struct {
From string `json:"from"`
To string `json:"to"`
Split string `json:"split"`
Timezone string `json:"timezone"`
Days *AnalyticsGenerateQueryDateDays `json:"days,omitempty"`
}

// WebsiteAnalyticsPointsPipeline mapping
type WebsiteAnalyticsPointsPipeline struct {
Aggregator *string `json:"aggregator,omitempty"`
// AnalyticsGenerateQueryDateDays mapping
type AnalyticsGenerateQueryDateDays struct {
Monday *AnalyticsGenerateQueryDateDay `json:"monday,omitempty"`
Tuesday *AnalyticsGenerateQueryDateDay `json:"tuesday,omitempty"`
Wednesday *AnalyticsGenerateQueryDateDay `json:"wednesday,omitempty"`
Thursday *AnalyticsGenerateQueryDateDay `json:"thursday,omitempty"`
Friday *AnalyticsGenerateQueryDateDay `json:"friday,omitempty"`
Saturday *AnalyticsGenerateQueryDateDay `json:"saturday,omitempty"`
Sunday *AnalyticsGenerateQueryDateDay `json:"sunday,omitempty"`
}

// WebsiteAnalyticsPointsPoint mapping
type WebsiteAnalyticsPointsPoint struct {
Value *uint32 `json:"value,omitempty"`
Hits *uint64 `json:"hits,omitempty"`
Date *WebsiteAnalyticsPointsPointDate `json:"date,omitempty"`
// AnalyticsGenerateQueryDateDay mapping
type AnalyticsGenerateQueryDateDay struct {
HourFrom string `json:"hour_from"`
HourTo string `json:"hour_to"`
}

// WebsiteAnalyticsPointsPointDate mapping
type WebsiteAnalyticsPointsPointDate struct {
From *uint64 `json:"from,omitempty"`
To *uint64 `json:"to,omitempty"`
// WebsiteAnalyticsGenerateData mapping
type WebsiteAnalyticsGenerateData struct {
Data *[]WebsiteAnalyticsGeneratePoint `json:"data,omitempty"`
}

// WebsiteAnalyticsFilterData mapping
type WebsiteAnalyticsFilterData struct {
Data *[]WebsiteAnalyticsFilter `json:"data,omitempty"`
}

// WebsiteAnalyticsFilter mapping
type WebsiteAnalyticsFilter struct {
Primary *string `json:"primary,omitempty"`
Secondary *string `json:"secondary,omitempty"`
Tertiary *string `json:"tertiary,omitempty"`
Aggregated *uint32 `json:"aggregated,omitempty"`
}

// WebsiteAnalyticsClassifierData mapping
type WebsiteAnalyticsClassifierData struct {
Data *[]WebsiteAnalyticsClassifier `json:"data,omitempty"`
}

// WebsiteAnalyticsClassifier mapping
type WebsiteAnalyticsClassifier struct {
Classifier *string `json:"classifier,omitempty"`
Aggregated *uint32 `json:"aggregated,omitempty"`
// WebsiteAnalyticsGeneratePoint mapping
type WebsiteAnalyticsGeneratePoint struct {
Period *string `json:"period,omitempty"`
Value *uint32 `json:"value,omitempty"`
SplitBy *string `json:"split_by,omitempty"`
Hits *uint32 `json:"hits,omitempty"`
UniqueHits *uint32 `json:"unique_hits,omitempty"`
SumValue *uint32 `json:"sum_value,omitempty"`
SumHits *uint32 `json:"sum_hits,omitempty"`
}


// String returns the string representation of WebsiteAnalyticsPoints
func (instance WebsiteAnalyticsPoints) String() string {
// String returns the string representation of WebsiteAnalyticsGeneratePoint
func (instance WebsiteAnalyticsGeneratePoint) String() string {
return Stringify(instance)
}

// String returns the string representation of WebsiteAnalyticsFilter
func (instance WebsiteAnalyticsFilter) String() string {
return Stringify(instance)
}


// AcquireAnalyticsPoints acquires analytics points for given type and metric in website.
func (service *WebsiteService) AcquireAnalyticsPoints(websiteID string, pointType string, pointMetric string, dateFrom time.Time, dateTo time.Time, dateSplit string, classifier string, filterPrimary string, filterSecondary string, filterTertiary string) (*WebsiteAnalyticsPoints, *Response, error) {
dateFromFormat, err := dateFrom.UTC().MarshalText()
if err != nil {
return nil, nil, err
}

dateToFormat, err := dateTo.UTC().MarshalText()
if err != nil {
return nil, nil, err
}

url := fmt.Sprintf("website/%s/analytics/%s/%s/points?date_from=%s&date_to=%s&date_split=%s&classifier=%s&filter_primary=%s&filter_secondary=%s&filter_tertiary=%s", websiteID, pointType, pointMetric, url.QueryEscape(string(dateFromFormat[:])), url.QueryEscape(string(dateToFormat[:])), url.QueryEscape(dateSplit), url.QueryEscape(classifier), url.QueryEscape(filterPrimary), url.QueryEscape(filterSecondary), url.QueryEscape(filterTertiary))

req, _ := service.client.NewRequest("GET", url, nil)
// GenerateAnalytics generates analytics for given type and metric in website.
func (service *WebsiteService) GenerateAnalytics(websiteID string, query AnalyticsGenerateQuery) (*[]WebsiteAnalyticsGeneratePoint, *Response, error) {
url := fmt.Sprintf("website/%s/analytics/generate", websiteID)
req, _ := service.client.NewRequest("POST", url, query)

points := new(WebsiteAnalyticsPointsData)
points := new(WebsiteAnalyticsGenerateData)
resp, err := service.client.Do(req, points)
if err != nil {
return nil, resp, err
}

return points.Data, resp, err
}


// ListAnalyticsFilters acquires available analytics filters in website.
func (service *WebsiteService) ListAnalyticsFilters(websiteID string, pageNumber uint, pointType string, pointMetric string, dateFrom time.Time, dateTo time.Time) (*[]WebsiteAnalyticsFilter, *Response, error) {
dateFromFormat, err := dateFrom.UTC().MarshalText()
if err != nil {
return nil, nil, err
}

dateToFormat, err := dateTo.UTC().MarshalText()
if err != nil {
return nil, nil, err
}

url := fmt.Sprintf("website/%s/analytics/%s/%s/filters/%d?date_from=%s&date_to=%s", websiteID, pointType, pointMetric, pageNumber, url.QueryEscape(string(dateFromFormat[:])), url.QueryEscape(string(dateToFormat[:])))
req, _ := service.client.NewRequest("GET", url, nil)

filters := new(WebsiteAnalyticsFilterData)
resp, err := service.client.Do(req, filters)
if err != nil {
return nil, resp, err
}

return filters.Data, resp, err
}


// ListAnalyticsClassifiers acquires available analytics classifiers in website.
func (service *WebsiteService) ListAnalyticsClassifiers(websiteID string, pageNumber uint, pointType string, pointMetric string, dateFrom time.Time, dateTo time.Time) (*[]WebsiteAnalyticsClassifier, *Response, error) {
dateFromFormat, err := dateFrom.UTC().MarshalText()
if err != nil {
return nil, nil, err
}

dateToFormat, err := dateTo.UTC().MarshalText()
if err != nil {
return nil, nil, err
}

url := fmt.Sprintf("website/%s/analytics/%s/%s/classifiers/%d?date_from=%s&date_to=%s", websiteID, pointType, pointMetric, pageNumber, url.QueryEscape(string(dateFromFormat[:])), url.QueryEscape(string(dateToFormat[:])))
req, _ := service.client.NewRequest("GET", url, nil)

classifiers := new(WebsiteAnalyticsClassifierData)
resp, err := service.client.Do(req, classifiers)
if err != nil {
return nil, resp, err
}

return classifiers.Data, resp, err
}

0 comments on commit 3cbb153

Please sign in to comment.