Shopify / buy-button-js

BuyButton.js is a highly customizable UI library for adding ecommerce functionality to any website.
http://shopify.github.io/buy-button-js/
MIT License
242 stars 114 forks source link

How to Pass Product Name, SKU and Price to Meta Pixel and GTM/GA4? #852

Open rplakas opened 1 year ago

rplakas commented 1 year ago

Does anyone know if its possible to pass the Product Name, SKU and Price to Meta Pixel to the Meta Pixel and GTM/GA4?

Where would I find the variable names? Here is my add-to-cart tracking code within the Buy Button.

    "DOMEvents": {
'click .shopify-buy__btn-wrapper .shopify-buy__btn': function (evt, target) {
    fbq('track', 'AddToCart', {
    content_name: prod_name, 
    content_ids: [prod_sku],
    content_type: 'product',
    value: prod_price,
    currency: 'USD'
 });
// Push the "add_to_cart" event data to the data layer
window.dataLayer.push({
  'event': 'add_to_cart',
  'ecommerce': {
    'add': {
      'items': [{
        'item_id': prod_sku,
        'item_name': prod_name,
        'price': prod_price,
        'currencyCode': 'USD'
rplakas commented 11 months ago

I figured out to do this with Individual Buy Buttons, still don't have it working with Collection Code though.

Here it is in case someone else needs it:

`"DOMEvents": { 'click .shopify-buybtn-wrapper .shopify-buybtn': function (evt, target) {

var prod_name = ui.components.product[0].model.title; var prod_qty = ui.components.product[0].selectedQuantity;
var prod_sku = ui.components.product[0].selectedVariant.sku; var prod_price = ui.components.product[0].selectedVariant.price.amount;

fbq('track', 'AddToCart', { content_name: prod_name, content_id: prod_sku, quantity: prod_qty,
content_type: 'product', value: prod_price, currency: 'USD' });`

rplakas commented 11 months ago

For whatever reason using

var prod_sku = ui.components.product[0].selectedVariant.title;

resulted in it passing "Default Title" instead of the actual product title.

rplakas commented 11 months ago

Does anyone know how to get this to work for Collection Code?

Using var prod_sku = ui.components.collectiont[0].selectedVariant.sku

results in an error in the console