Hello
I’m trying to develop a pixel extension for our web site.
Pixel is going to do the following things
- Add a js code to all page (so we can cookie when someone visits the shop with our ref code)
- Add pixel code to the thank you page
echo “API Response:\n”; part returns empty (no error or something)
Notes
- Our store is development store which we have created from shopify partner web site.
- 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);