Web pixel empty response

Hello

I’m trying to develop a pixel extension for our web site.

Pixel is going to do the following things

  1. Add a js code to all page (so we can cookie when someone visits the shop with our ref code)
  2. Add pixel code to the thank you page

echo “API Response:\n”; part returns empty (no error or something)

Notes

  1. Our store is development store which we have created from shopify partner web site.
  2. When i finish the pixel , i will submit that to app store
<?php
$shop = "your-store.myshopify.com"; // Shopify  URL
$accessToken = "your-access-token";  // OAuth token

$merchantId = 3; //  merchant ID

// JS kodu içine merchantId inject
$js_code = <<<JS
analytics.subscribe('page_viewed', async (event) => {
  const script = document.createElement('script');
  script.setAttribute('src', 'https://www.example.com/pixel.js');
  script.setAttribute('async', '');
  script.setAttribute('data-merchant-id', '$merchantId');
  document.head.appendChild(script);
});

analytics.subscribe('checkout_completed', async (event) => {
  if (!event?.data?.checkout) return;
});
JS;

// GraphQL mutation
$query = <<<GRAPHQL
mutation webPixelCreate(\$webPixel: WebPixelInput!) {
  webPixelCreate(webPixel: \$webPixel) {
    userErrors { field message code }
    webPixel { id settings }
  }
}
GRAPHQL;

// Variables array
$variables = [
    "webPixel" => [
        "name" => "Custom Referrals Pixel",
        "settings" => [
            "script" => $js_code
        ]
    ]
];

// cURL ile GraphQL isteği
$ch = curl_init("https://$shop/admin/api/unstable/graphql.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "X-Shopify-Access-Token: $accessToken"
]);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
    "query" => $query,
    "variables" => $variables
]));

$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo "cURL error: " . curl_error($ch);
} else {
    echo "API Response:\n";
    print_r(json_decode($response, true));
}

curl_close($ch);

adding a basic javascript to the pages should not be that difficult.

it’s not rocket sciene at all

Hey there,

I think you’re implementing a custom pixel on your Shopify store. The issue seems to be that the API response is empty.

This could be because of the following:

  • An incorrect API endpoint usage.

  • Issues with the GraphQL mutation.

Ensure your access token and shop URL are correct, and verify the GraphQL query structure.

Look into development store settings and allow for such custom scripts.

Read this for further understanding: https://shopify.dev/docs/api

thank you for the reply. when i check the webPixelCreate - GraphQL Admin , i do not see any issue with the code . documentation does not mention any empty response

does anybody see issue?

it’s difficult to understand why shopify chooses to be so developer-unfriendly

adding a simple javascript code shouldn’t be this difficult