我试图回应Google Analytics中的php变量以进行电子商务跟踪。根据我的理解,php运行"服务器端",Javascript在浏览器的客户端运行。为了让他们一起工作,可能需要AJAX(但这超出了我的技能)。
这是我到目前为止所拥有的:
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-1', 'auto');
ga('send', 'pageview');
ga('require', 'ecommerce', 'ecommerce.js');
ga('ecommerce:addTransaction', {
id: '<?php echo $orderNumber;?>',
affiliation: '<?php echo $orderAffiliateId;?>',
revenue: '<?php echo $orderTotal;?>',
shipping: '' ,
tax: '' });
ga('ecommerce:addItem', {
id: '<?php echo $productId;?>',
sku: '<?php echo $productSku;?>',
product name: '<?php echo $productName;?>',
category: '', // Pass the relevant category value
price: '<?php echo $productAmount;?>',
quantity: '1'});
ga('ecommerce:send');
</script>
当我在&#34;订单确认&#34;上检查源代码时页面,我看到PHP代码而不是我尝试发送给Google Analytics的值。我做错了什么?
请仔细阅读您的解释,因为我的技能水平充其量只是初学者。
上面的代码嵌入在WordPress页面的区域中。我正在使用示例&#34;推送通知脚本&#34; (由会员插件提供)。我唯一做的就是在php文件的底部添加Google Analytics脚本(在文件底部附近的脚本中看到)。
<?php
// This file demonstrates how to handle different payment events and access the data passed to the script by MemberMouse.
// ---- GET EVENT TYPE ----
if(!isset($_GET["event_type"]))
{
// event type was not found, so exit
exit;
}
else
{
$eventType = $_GET["event_type"];
}
// ---- ACCESS DATA ----
// member data
$memberId = $_GET["member_id"];
$registeredDate = $_GET["registered"];
$lastLoginDate = $_GET["last_logged_in"];
$lastUpdatedDate = $_GET["last_updated"];
$daysAsMember = $_GET["days_as_member"];
$status = $_GET["status"];
$statusName = $_GET["status_name"];
$membershipLevelId = $_GET["membership_level"];
$membershipLevelName = $_GET["membership_level_name"];
$isComplimentary = $_GET["is_complimentary"];
$username = $_GET["username"];
$email = $_GET["email"];
$phone = $_GET["phone"];
$firstName = $_GET["first_name"];
$lastName = $_GET["last_name"];
$billingAddress = $_GET["billing_address"];
$billingCity = $_GET["billing_city"];
$billingState = $_GET["billing_state"];
$billingZipCode = $_GET["billing_zip_code"];
$billingCountry = $_GET["billing_country"];
$shippingAddress = $_GET["shipping_address"];
$shippingCity = $_GET["shipping_city"];
$shippingState = $_GET["shipping_state"];
$shippingZipCode = $_GET["shipping_zip_code"];
$shippingCountry = $_GET["shipping_country"];
// custom field data
// You can access custom field data by accessing the get parameter cf_# where # is the
// ID of the custom field
//$exampleCustomData = $_GET["cf_1"];
// order data
$orderNumber = $_GET["order_number"];
$orderTransactionId = $_GET["order_transaction_id"];
$orderTotal = $_GET["order_total"];
$orderSubtotal = $_GET["order_subtotal"];
$orderDiscount = $_GET["order_discount"];
$orderShipping = $_GET["order_shipping"];
$orderShippingMethod = $_GET["order_shipping_method"];
$orderBillingAddress = $_GET["order_billing_address"];
$orderBillingCity = $_GET["order_billing_city"];
$orderBillingState = $_GET["order_billing_state"];
$orderBillingZipCode = $_GET["order_billing_zipcode"];
$orderBillingCountry = $_GET["order_billing_country"];
$orderShippingAddress = $_GET["order_shipping_address"];
$orderShippingCity = $_GET["order_shipping_city"];
$orderShippingState = $_GET["order_shipping_state"];
$orderShippingZipCode = $_GET["order_shipping_zipcode"];
$orderShippingCountry = $_GET["order_shipping_country"];
$orderAffiliateId = $_GET["order_affiliate_id"];
$orderSubaffiliateId = $_GET["order_subaffiliate_id"];
$orderIPAddress = $_GET["order_ip_address"];
// access products associated with the order
$products = json_decode(stripslashes($_GET["order_products"]));
foreach($products as $product)
{
$productId = $product->id;
$productName = $product->name;
$productSku = $product->sku;
$productAmount = $product->amount;
$productQuantity = $product->quantity;
$productTotal = $product->total; // amount * quantity
$productIsRecurring = $product->is_recurring; // true, false
$productRecurringAmount = $product->recurring_amount; // amount charged every rebill period
$productRebillPeriod = $product->rebill_period; // integer - complete rebill period is a combination of rebill period
// and frequency i.e. 1 months, 30 days, 2 weeks, etc.
$productRebillFrequency = $product->rebill_frequency; // days, weeks, months, years
}
// access coupons associated with the order
$coupons = json_decode(stripslashes($_GET["order_coupons"]));
foreach($coupons as $coupon)
{
$couponId = $coupon->id;
$couponName = $coupon->name;
$couponCode = $coupon->code;
}
// ---- EVENT TYPES ----
$PAYMENT_RECEIVED = "mm_payment_received";
$PAYMENT_REBILL = "mm_payment_rebill";
$REFUND_ISSUED = "mm_refund_issued";
// ---- PERFORM ACTION BASED ON EVENT TYPE ----
switch($eventType)
{
case $PAYMENT_RECEIVED:
// do something
break;
case $PAYMENT_REBILL:
// do something
break;
case $REFUND_ISSUED:
// do something
break;
}
?>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-XXXXXXXX-1', 'auto');
ga('send', 'pageview');
ga('require', 'ecommerce', 'ecommerce.js');
ga('ecommerce:addTransaction', {
id: '<?php echo $orderNumber;?>',
affiliation: '<?php echo $orderAffiliateId;?>',
revenue: '<?php echo $orderTotal;?>',
shipping: '' ,
tax: '' });
ga('ecommerce:addItem', {
id: '<?php echo $productId;?>',
sku: '<?php echo $productSku;?>',
product name: '<?php echo $productName;?>',
category: '', // Pass the relevant category value
price: '<?php echo $productAmount;?>',
quantity: '1'});
ga('ecommerce:send');
</script>
&#13;