Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

analytics.track JS appended to API responses breaks some implementations #194

Open
bobmurdoch opened this issue Dec 28, 2022 · 6 comments
Open

Comments

@bobmurdoch
Copy link

For some time I've used
utils.api.cart.getContent({ template: 'custom/json/cart-product-ids' ... }
with the following code in that template
{{ json cart.items }}
So I can take the response inside the callback of utils.api.cart.getContent and parse json to get that cart contents.

Sometime yesterday the server started appending a <script> tag the end of the response with analytics.track('Product Added'... and so on inside which broke our site/ability to put things in the cart since the script is trying to parse the response as JSON.

Is there some way we can send an option parameter to ask for a json response (vs html) or somehow as the library here to not append that script tag as if the response were HTML? If this exists in the options parameter now, it would be great to see some explanation of what those options are in the docs since
https://developer.bigcommerce.com/stencil-docs/ZG9jOjIyMDcyMA-stencil-utils#get-cart

Thanks

@bc-jz
Copy link
Contributor

bc-jz commented Dec 28, 2022

Hi @minutiae ,

Thank you for the report. This sounds unintentional on our end as the content returned ought to just be as defined by contents of your template file (cart-product-ids). I am not seeing a similar script appended to my own results when testing similar behavior. It is possible I am not passing the same options as you in my getContent() call. Could you reply with the entirety of the getContent call that you are making within your script? I would like to try and reproduce what you are doing which should help us track down why this has appeared.

Is your script running in a certain scenario? Such as following an add to cart action?

Thank you for the added details.

@bobmurdoch
Copy link
Author

I've noticed in my local environment the script tag does not get appended, only in production.

Yes, this follows an add to cart call from stencil utils. I thought perhaps this might occur only on the call following the add to cart call as a kind of queued message that needs to get out once, so I tried calling get cart a second time inside of the callback of the initial get cart call but both responses contain the script tag.

Here is what my code looks like now that I've put in a temp fix to strip out the <script> tag @bc-jz ...

utils.api.cart.getContent({ template: 'custom/json/cart-product-ids' }, (err, response) => {
  let sanitizedString = response;
  if (response.indexOf('<script') !== -1) {
      sanitizedString = response.substring(0, response.indexOf('<script'));
  }
  const cartItems = JSON.parse(sanitizedString.replace(/&quot;/g, '"'));
  .... do some work with cartItems
});

@bc-jz
Copy link
Contributor

bc-jz commented Dec 28, 2022

Thank you for the added details. I will be digging into this further but we do have an idea about where to look now. I will be getting back to you once I have some more information.

@bc-jz
Copy link
Contributor

bc-jz commented Jan 3, 2023

Hi @minutiae,

I just wanted to let you know I am still working on this. Things have been delayed due to the holidays and I have also not been able to reproduce this same issue on my own store. I am assuming I am doing something wrong.

Could you clarify the following when you get a chance?

The custom template file that you have at custom/json/cart-product-ids - is the entirety of it's contents just the one line that you referenced:

{{ json cart.items }}

?

One of the teams related to the changes in this area should be back this week which will help me to dig further. Thank you for your patience in the meantime.

@bobmurdoch
Copy link
Author

@bc-jz That's correct, with a blank line following it since my editor appends a single blank line automatically.

{{ json cart.items }}

@bobmurdoch
Copy link
Author

@bc-jz I'm not sure if these two are connected, but one thing that may cause some differing behavior in stores is whether google analytics enhanced ecommerce tracking is turned on, I believe that is what controls settings.data_tag_enabled on the theme side. Could be that the tracking info being appended to my requests are contingent on that being on (it is on for our store), but I could be wrong. Just a thought

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants