Analytics - Tracking Amp-State
Is there currently a way to track Amp-State object variables? For example, If I were able to bind to [data-vars-product-id]="productState.productId"? Or somehow to bind the analytics config json variables (vars) to amp-state object variables.
Ultimately I need to pull back specific product data so that I know which sku a User clicks 'add to cart' on, not just that they click the 'add to cart' button on a product's detail page. I have not found anything that gives amp analytics access to my amp-states. Any help or guidance is greatly appreciated
/to @jeffjose /cc @zhouyx @choumx
If I were able to bind to [data-vars-product-id]="productState.productId"
This sounds the approach to go. @choumx <amp-analytics> already supports data-vars attribute Will the productState.productId value be resolved to the id value?
Yes we initialize the *productState.productId with a value, and as that value changes with user interaction (sku selection), we want to know which exact sku the User adds to their cart by skuId.
*(it should be 'productState.skuId', forgive my example using 'productId')
Does this help? https://github.com/ampproject/amphtml/blob/master/spec/amp-var-substitutions.md#amp-state
Great! Didn't know we have support for AMP_STATE.
@bthomas16 you should be able to apply data-vars-product-id=AMP_STATE(productState, productId) the the clickable element. Or simply appending the param productId=AMP_STATE(productState, productId) to the extraUrlParams or the request itself.
Please let us know if that works for you. Thanks.
This is great! I was looking for the wrong things online, but now I don't know how I missed it.
Thank you so much for the help!
Last question,
Above I was providing a simple example for "skuState.skuId". In reality, I am now struggling to target the actual sku id's because the state looks like this:
<amp-state id="skuState">
<script type="application/json">
{
"currentSku": 'sku1',
"sku1":
{
"skuId": 2,
"price": 50
},
"sku2":
{
"skuId": 2,
"price": 50
}
}
</script>
</amp-state>
As a user clicks on a different sku, we change the currentSku value, which then updates that newly selected sku's properties across the pdp. However, tracking the selected sku's properties is proving difficult with data-vars-sku-id=AMP_STATE(skuState[skuState.currentSkuState].skuId).
In other areas on the page, we bind to the selected sku's propeties as such: [value]="skuState[skuState.currentSkuState].price".
Currently, data-vars-sku-id=AMP_STATE(skuState.currentSku) returns // sku1 (as expected), but beyond that I have not been able to target properties within amp-state based on existing amp-state properties.
AMP_STATE() only takes JSON expressions (e.g. x.y.z), so you'll need to copy whatever data you need to track such that it can be referenced by a JSON expression.
Hi, can this also be used inside an amp-analytics json tag such as this: https://amp.dev/documentation/guides-and-tutorials/optimize-and-measure/configure-analytics/deep_dive_analytics/?format=websites
@AndrewKGuan, AMP_STATE or ${ampState} should work as any variable.