@@ -50,6 +50,7 @@ type State = {
5050
5151export default class Pdp extends Component < DefaultProps , Props , State > {
5252 _productVariants : ProductVariants ;
53+ _productViewTracked : boolean = false ;
5354
5455 state : State = {
5556 currentSku : null ,
@@ -60,6 +61,14 @@ export default class Pdp extends Component<DefaultProps, Props, State> {
6061 t : _ . identity ,
6162 } ;
6263
64+ componentDidMount ( ) {
65+ this . trackProductView ( ) ;
66+ }
67+
68+ componentWillReceiveProps ( nextProps : Props ) {
69+ this . trackProductView ( nextProps ) ;
70+ }
71+
6372 get isArchived ( ) : boolean {
6473 return ! ! _ . get ( this . props , [ 'product' , 'archivedAt' ] ) ;
6574 }
@@ -68,13 +77,33 @@ export default class Pdp extends Component<DefaultProps, Props, State> {
6877 return this . state . currentSku || this . sortedSkus [ 0 ] ;
6978 }
7079
71- get sortedSkus ( ) : Array < Sku > {
80+ getSortedSkus ( props : Props = this . props ) : Array < Sku > {
7281 return _ . sortBy (
73- _ . get ( this . props , 'product.skus' , [ ] ) ,
82+ _ . get ( props , 'product.skus' , [ ] ) ,
7483 'attributes.salePrice.v.value'
7584 ) ;
7685 }
7786
87+ get sortedSkus ( ) : Array < Sku > {
88+ return this . getSortedSkus ( ) ;
89+ }
90+
91+ trackProductView ( props : Props = this . props ) {
92+ if ( this . _productViewTracked ) return ;
93+
94+ const {
95+ isLoading ,
96+ notFound ,
97+ fetchError ,
98+ product
99+ } = props ;
100+
101+ if ( ! isLoading && ! notFound && ! fetchError && product ) {
102+ tracking . viewDetails ( this . getProductView ( props ) ) ;
103+ this . _productViewTracked = true ;
104+ }
105+ }
106+
78107 @autobind
79108 setCurrentSku ( currentSku : Sku ) {
80109 this . setState ( { currentSku } ) ;
@@ -87,12 +116,12 @@ export default class Pdp extends Component<DefaultProps, Props, State> {
87116 this . setState ( assoc ( this . state , namePath , stateValue ) ) ;
88117 }
89118
90- get productView ( ) : TProductView {
91- const attributes = _ . get ( this . props . product , 'attributes' , { } ) ;
119+ getProductView ( props : Props = this . props ) : TProductView {
120+ const attributes = _ . get ( props . product , 'attributes' , { } ) ;
92121 const price = _ . get ( this . currentSku , 'attributes.salePrice.v' , { } ) ;
93122 let images = _ . get ( this . currentSku , [ 'albums' , '0' , 'images' ] , [ ] ) ;
94123 if ( _ . isEmpty ( images ) ) {
95- images = _ . get ( this . props . product , [ 'albums' , '0' , 'images' ] , [ ] ) ;
124+ images = _ . get ( props . product , [ 'albums' , '0' , 'images' ] , [ ] ) ;
96125 }
97126 const imageUrls = images . map ( image => image . src ) ;
98127
@@ -102,10 +131,14 @@ export default class Pdp extends Component<DefaultProps, Props, State> {
102131 images : imageUrls ,
103132 currency : _ . get ( price , 'currency' , 'USD' ) ,
104133 price : _ . get ( price , 'value' , 0 ) ,
105- skus : this . sortedSkus ,
134+ skus : this . getSortedSkus ( props ) ,
106135 } ;
107136 }
108137
138+ get productView ( ) : TProductView {
139+ return this . getProductView ( ) ;
140+ }
141+
109142 get productShortDescription ( ) : ?Element < any > {
110143 const shortDescription = _ . get ( this . props . product , 'attributes.shortDescription.v' ) ;
111144
0 commit comments