flamingo.me/flamingo-commerce/v3@v3.11.0/w3cdatalayer/domain/datalayer.go (about)

     1  package domain
     2  
     3  import (
     4  	"encoding/gob"
     5  	"encoding/json"
     6  )
     7  
     8  type (
     9  	// DatalayerProvider func
    10  	DatalayerProvider func() *Datalayer
    11  
    12  	// Datalayer Value object - represents the structure of the w3c Datalayer.
    13  	// Therefore it has the json annotations and its intended to be directly converted to Json in the output
    14  	Datalayer struct {
    15  		PageInstanceID string
    16  		Page           *Page
    17  		SiteInfo       *SiteInfo
    18  		Version        string `inject:"config:commerce.w3cDatalayer.version,optional"` // todo: version should not be injected here (domain layer)
    19  		//User List of user(s) interacting with the page. (Although typically web data has a single user per recorded interaction, this object is an array and can capture multiple users.)
    20  		User []User
    21  		//The Cart object carries details about a shopping cart or basket and the products that have been added to it.
    22  		Cart *Cart
    23  		// The Event object collects information about an interaction event by the user. An event might be a button click, the addition of a portal widget, playing a video, adding a product to the shopping cart, etc. Any action on the page could be captured by an Event object.
    24  		Event []Event
    25  		//The Product object carries details about a particular product with frequently used properties listed below. This is intended for data about products displayed on pages or other content. For products added to a shopping cart or ordered in a transaction, see the Cart and Transaction objects below.
    26  		Product []Product
    27  		//The Transaction object is similar to the Cart object, but represents a completed order. The Transaction object contains analogous sub-objects to the Cart object as well as additional subobjects specific to completed orders.
    28  		Transaction *Transaction
    29  	}
    30  
    31  	// Product struct
    32  	Product struct {
    33  		ProductInfo ProductInfo            `json:"productInfo"`
    34  		Category    *ProductCategory       `json:"category,omitempty"`
    35  		Attributes  map[string]interface{} `json:"attributes,omitempty"`
    36  	}
    37  
    38  	// Event struct
    39  	Event struct {
    40  		EventInfo map[string]interface{} `json:"eventInfo,omitempty"`
    41  	}
    42  
    43  	// Page struct
    44  	Page struct {
    45  		PageInfo   PageInfo               `json:"pageInfo,omitempty"`
    46  		Category   PageCategory           `json:"category,omitempty"`
    47  		Search     SearchInfo             `json:"search,omitempty"`
    48  		Attributes map[string]interface{} `json:"attributes,omitempty"`
    49  	}
    50  
    51  	// SearchInfo struct
    52  	SearchInfo struct {
    53  		SearchKeyword string      `json:"searchKeyword,omitempty"`
    54  		Result        interface{} `json:"result,omitempty"`
    55  	}
    56  
    57  	// PageInfo generall information about the page
    58  	PageInfo struct {
    59  		PageID         string `json:"pageID,omitempty"`
    60  		DestinationURL string `json:"destinationURL,omitempty"`
    61  		BreadCrumbs    string `json:"breadCrumbs,omitempty"`
    62  		PageName       string `json:"pageName,omitempty"`
    63  		ReferringURL   string `json:"referringUrl,omitempty"`
    64  		Language       string `json:"language,omitempty"`
    65  		ErrorName      string `json:"errorName,omitempty"`
    66  	}
    67  
    68  	// PageCategory struct for the category and subcategories
    69  	PageCategory struct {
    70  		PrimaryCategory string `json:"primaryCategory"`
    71  		SubCategory1    string `json:"subCategory1,omitempty"`
    72  		SubCategory2    string `json:"subCategory2,omitempty"`
    73  		PageType        string `json:"pageType,omitempty"`
    74  		Section         string `json:"section,omitempty"`
    75  	}
    76  
    77  	// SiteInfo - struct for SiteName and Domain
    78  	SiteInfo struct {
    79  		SiteName string `json:"siteName,omitempty"`
    80  		Domain   string `json:"domain,omitempty"`
    81  	}
    82  
    83  	// User The User object captures the profile of a user who is interacting with the website.
    84  	User struct {
    85  		/**
    86  		Profile
    87  		A profile for information about the user, typically associated with a registered user. (Although
    88  		typically a user might have only a single profile, this object is an array and can capture multiple
    89  		profiles per user.)
    90  		*/
    91  		Profile []UserProfile `json:"profile,omitempty"`
    92  		/**
    93  		Segment This object provides population segmentation information for the user, such as premium versus
    94  		basic membership, or any other forms of segmentation that are desirable. Any additional
    95  		dimensions related to the user can be provided. All names are optional and should fit the
    96  		individual implementation needs in both naming and values passed.
    97  		*/
    98  		Segment string `json:"segment,omitempty"`
    99  	}
   100  
   101  	// UserProfile A profile for information about the user
   102  	UserProfile struct {
   103  		ProfileInfo     UserProfileInfo `json:"profileInfo,omitempty"`
   104  		Address         *Address        `json:"address,omitempty"`
   105  		ShippingAddress *Address        `json:"shippingAddress,omitempty"`
   106  	}
   107  
   108  	// Address basic address information
   109  	Address struct {
   110  		Line1         string `json:"line1,omitempty"`
   111  		Line2         string `json:"line2,omitempty"`
   112  		City          string `json:"city,omitempty"`
   113  		StateProvince string `json:"stateProvince,omitempty"`
   114  		PostalCode    string `json:"postalCode,omitempty"`
   115  		Country       string `json:"country,omitempty"`
   116  	}
   117  
   118  	// UserProfileInfo An extensible object for providing information about the user.
   119  	UserProfileInfo struct {
   120  		EmailID   string `json:"emailID,omitempty"`
   121  		UserName  string `json:"userName,omitempty"`
   122  		ProfileID string `json:"profileID"`
   123  		Rewards   string `json:"rewards,omitempty"`
   124  	}
   125  
   126  	// Cart cartInformation
   127  	Cart struct {
   128  		CartID     string                 `json:"cartID,omitempty"`
   129  		Price      *CartPrice             `json:"price,omitempty"`
   130  		Attributes map[string]interface{} `json:"attributes,omitempty"`
   131  		Item       []CartItem             `json:"item,omitempty"`
   132  	}
   133  
   134  	// CartPrice used in Cart
   135  	CartPrice struct {
   136  		//The basePrice SHOULD be the price of the items before applicable discounts,shipping charges, and tax.
   137  		BasePrice       float64 `json:"basePrice"`
   138  		VoucherCode     string  `json:"voucherCode"`
   139  		VoucherDiscount float64 `json:"voucherDiscount"`
   140  		Currency        string  `json:"currency"`
   141  		TaxRate         float64 `json:"taxRate"`
   142  		Shipping        float64 `json:"shipping"`
   143  		ShippingMethod  string  `json:"shippingMethod"`
   144  		PriceWithTax    float64 `json:"priceWithTax"`
   145  		//cartTotal SHOULD be the total price inclusive of all discounts, charges, and tax
   146  		CartTotal float64 `json:"cartTotal"`
   147  	}
   148  
   149  	// CartItem used in Cart
   150  	CartItem struct {
   151  		ProductInfo ProductInfo            `json:"productInfo"`
   152  		Quantity    int                    `json:"quantity"`
   153  		Category    *ProductCategory       `json:"category,omitempty"`
   154  		Price       CartItemPrice          `json:"price"`
   155  		Attributes  map[string]interface{} `json:"attributes,omitempty"`
   156  	}
   157  
   158  	// CartItemPrice struct
   159  	CartItemPrice struct {
   160  		BasePrice    float64 `json:"basePrice"`
   161  		Currency     string  `json:"currency"`
   162  		TaxRate      float64 `json:"taxRate"`
   163  		PriceWithTax float64 `json:"priceWithTax"`
   164  	}
   165  
   166  	// Transaction struct
   167  	Transaction struct {
   168  		TransactionID string                 `json:"transactionID,omitempty"`
   169  		Profile       *UserProfile           `json:"profile,omitempty"`
   170  		Price         *TransactionPrice      `json:"total,omitempty"`
   171  		Item          []CartItem             `json:"item,omitempty"`
   172  		Attributes    map[string]interface{} `json:"attributes,omitempty"`
   173  	}
   174  
   175  	// TransactionPrice struct
   176  	TransactionPrice struct {
   177  		//The basePrice SHOULD be the price of the items before applicable discounts,shipping charges, and tax.
   178  		BasePrice        float64 `json:"basePrice"`
   179  		VoucherCode      string  `json:"voucherCode"`
   180  		VoucherDiscount  float64 `json:"voucherDiscount"`
   181  		Currency         string  `json:"currency"`
   182  		TaxRate          float64 `json:"taxRate"`
   183  		Shipping         float64 `json:"shipping"`
   184  		ShippingMethod   string  `json:"shippingMethod"`
   185  		PriceWithTax     float64 `json:"priceWithTax"`
   186  		TransactionTotal float64 `json:"transactionTotal"`
   187  	}
   188  
   189  	// ProductCategory struct
   190  	ProductCategory struct {
   191  		PrimaryCategory string `json:"primaryCategory,omitempty"`
   192  		SubCategory1    string `json:"subCategory1,omitempty"`
   193  		SubCategory     string `json:"subCategory,omitempty"`
   194  		SubCategory2    string `json:"subCategory2,omitempty"`
   195  		ProductType     string `json:"productType,omitempty"`
   196  	}
   197  
   198  	// ProductInfo dataLayer product information
   199  	ProductInfo struct {
   200  		ProductID   string `json:"productID"`
   201  		SKU         string `json:"sku"`
   202  		ProductName string `json:"productName"`
   203  		//ProductURL               string  `json:"productURL"`
   204  		ProductImage             string  `json:"productImage"`
   205  		ProductThumbnail         string  `json:"productThumbnail"`
   206  		Manufacturer             string  `json:"manufacturer"`
   207  		Size                     string  `json:"size"`
   208  		Color                    string  `json:"color"`
   209  		ParentID                 *string `json:"parentId,omitempty"`
   210  		VariantSelectedAttribute *string `json:"variantSelectedAttribute,omitempty"`
   211  		ProductType              string  `json:"productType"`
   212  		Retailer                 string  `json:"retailer"`
   213  		Brand                    string  `json:"brand"`
   214  		InStock                  string  `json:"inStock"`
   215  	}
   216  )
   217  
   218  func init() {
   219  	gob.Register(Event{})
   220  }
   221  
   222  // MarshalJSON - is here to make sure the renderingengine uses this json interface instead of own encoding
   223  func (d Datalayer) MarshalJSON() ([]byte, error) {
   224  	//myDataLayer should match the Datalayer struct and is just here to define the top level json marshal annotations
   225  	// we need this since json.Marshal(&d) would result in endless loop
   226  	type myDataLayer struct {
   227  		PageInstanceID string    `json:"pageInstanceID"`
   228  		Page           *Page     `json:"page,omitempty"`
   229  		SiteInfo       *SiteInfo `json:"siteInfo,omitempty"`
   230  		Version        string    `json:"version"`
   231  		//User List of user(s) interacting with the page. (Although typically web data has a single user per recorded interaction, this object is an array and can capture multiple users.)
   232  		User []User `json:"user"`
   233  		//The Cart object carries details about a shopping cart or basket and the products that have been added to it.
   234  		Cart *Cart `json:"cart,omitempty"`
   235  		// The Event object collects information about an interaction event by the user. An event might be a button click, the addition of a portal widget, playing a video, adding a product to the shopping cart, etc. Any action on the page could be captured by an Event object.
   236  		Event []Event `json:"event,omitempty"`
   237  		//The Product object carries details about a particular product with frequently used properties listed below. This is intended for data about products displayed on pages or other content. For products added to a shopping cart or ordered in a transaction, see the Cart and Transaction objects below.
   238  		Product []Product `json:"product,omitempty"`
   239  		//The Transaction object is similar to the Cart object, but represents a completed order. The Transaction object contains analogous sub-objects to the Cart object as well as additional subobjects specific to completed orders.
   240  		Transaction *Transaction `json:"transaction,omitempty"`
   241  	}
   242  	myDataLayerInstance := myDataLayer(d)
   243  	return json.Marshal(&myDataLayerInstance)
   244  }