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

Call s.js method from PHP #8283

Conversation

psealock
Copy link
Contributor

@psealock psealock commented Dec 1, 2017

This is a pull request onto an ongoing pull request #8123

cc @greenafrican

Main Change

Instead of placing information in a javascript object on the page for the Analytics repo to read, call the Analytics methods directly. Reasons for this include:

  • Avoid having the analytics library include logic on when to fire an event or not.
  • Allow add-to-cart events that use an AJAX request to fire an analytics event without a page refresh

Other Changes

  • Add a product page view event on "add-to-cart" from non-product page in addition to add-to-cart event.
  • Use add_action( 'woocommerce_add_to_cart' ...) to capture a product added via a product page.
  • Create a handle_purchase_event function. (Currently needs work)
  • Change the API of the Analytics library from track to push so it can happen before or after initialization of s.js

Issues

The ajax call to add a product to the cart can come from a cached page. This means any cart information coming from PHP can't be reliable. A cart hash and session id are available via cookies, but information on the contents of the cart aren't not. I see three possible solutions:

  1. Fire add-to-cart events from PHP and keep track of cart details in session storage. The problem here is cookies such as tk_rl, tk_ro, tk_ai won't be available.
  2. Sync cart tables via Jetpack and use cart hashes to match events with cart contents in the Tracks ETL process.
  3. Keep cart details in cookies to be retrieved anytime.

@psealock psealock requested a review from a team as a code owner December 1, 2017 04:12
@greenafrican
Copy link
Contributor

  1. Fire add-to-cart events from PHP and keep track of cart details in session storage. The problem here is cookies such as tk_rl, tk_ro, tk_ai won't be available.

I like this. The cookies are set via s.js and should be available, even on cached pages. They should be in the request header. This seemed to work when I tested it:
screen shot 2017-12-01 at 09 57 59

  1. Sync cart tables via Jetpack and use cart hashes to match events with cart contents in the Tracks ETL process.

The cart tables are deleted after a few days, which would sync delete them too on wp.com. I feel we may benefit from keeping all the tracking together but perhaps we should think about this more if 1 or 3 don't work.

  1. Keep cart details in cookies to be retrieved anytime.

We could wait for the response from ?wc-ajax=add_to_cart, which includes the cart hash and send the cart event? We could even create our own cart id/hash and manage everything in cookies and js.

@psealock
Copy link
Contributor Author

psealock commented Dec 1, 2017

I like this. The cookies are set via s.js and should be available, even on cached pages. They should be in the request header.

Thanks, did not think about this. We'd have to make sure cookies are set before a user initiates the ajax request.

Seems we could try #1 first. Since a session can have multiple carts, we'll need a mechanism for id'ing carts. A purchase event would also need to have access to the cart id (I think?). Maybe fire that one from PHP as well?

@psealock
Copy link
Contributor Author

psealock commented Dec 6, 2017

Closing in favour of #8296

@psealock psealock closed this Dec 6, 2017
@jeherve jeherve deleted the ry/tracks-store-stats-call-lib-from-PHP branch December 6, 2017 10:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants