amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Analytics - Tracking Amp-State

Open brentthomascodes opened this issue 8 years ago • 10 comments

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

brentthomascodes avatar Sep 17 '18 15:09 brentthomascodes

/to @jeffjose /cc @zhouyx @choumx

aghassemi avatar Sep 17 '18 16:09 aghassemi

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?

zhouyx avatar Sep 17 '18 16:09 zhouyx

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')

brentthomascodes avatar Sep 17 '18 17:09 brentthomascodes

Does this help? https://github.com/ampproject/amphtml/blob/master/spec/amp-var-substitutions.md#amp-state

dreamofabear avatar Sep 18 '18 17:09 dreamofabear

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.

zhouyx avatar Sep 18 '18 23:09 zhouyx

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!

brentthomascodes avatar Sep 19 '18 03:09 brentthomascodes

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.

brentthomascodes avatar Sep 19 '18 17:09 brentthomascodes

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.

dreamofabear avatar Sep 19 '18 17:09 dreamofabear

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 avatar Oct 04 '19 21:10 AndrewKGuan

@AndrewKGuan, AMP_STATE or ${ampState} should work as any variable.

zhouyx avatar Oct 05 '19 01:10 zhouyx