0

我正在使用 PHP 开发 Google 电子商务跟踪类。此类的目的是创建 URL 以使用 cURL 发送到 Google Analytics。

我首先创建一个带有一些参数的事务(url),例如:

  • client_id
  • UA
  • transaction_id
  • 联系

然后我向它添加交易项目(产品是购物车)。

我希望该项目继承上述前四个参数。这样我就不需要每次都重新设置它们。

电子商务跟踪类

class EcommerceTracking {

    protected $_urls = array();
    protected $_items = array();

    /* 
     * The Protocol version. The current value is '1'. 
     * This will only change when there are changes made that are not backwards compatible. 
     */
    protected $_v = '1';

    /* 
     * The tracking ID / web property ID. The format is UA-XXXX-Y. 
     * All collected data is associated by this ID. 
     */
    protected $_tid = '';   

    /* 
     * This anonymously identifies a particular user, device, or browser instance. 
     * For the web, this is generally stored as a first-party cookie with a two-year expiration. 
     */
    protected $_cid = 0;

    /* 
     * The type of hit 
     */
    protected $_t = 'transaction';

    /* A unique identifier for the transaction. This value should be the same for both the 
     * Transaction hit and Items hits associated to the particular transaction. 
     */
    protected $_ti = 0; 

    /* 
     * Specifies the affiliation or store name. 
     */
    protected $_ta = 'online';

    /*
     * Specifies the total revenue associated with the transaction. 
     */
    protected $_tr = 0;

    /*
     * Specifies the total shipping cost of the transaction. 
     */
    protected $_ts = 0;

    /*
     * Specifies the total tax of the transaction.
     */
    protected $_tt = 0;

    /*
     * When present indicates the local currency for all transaction currency values. 
     * Value should be a valid ISO 4217 currency code.
     */
    protected $_cu = 'EUR';


    public function __contruct( $params ) {
        $this->_tid     = $params['ua'];
        $this->_cid     = $params['client_id'];
        $this->_ti  = $params['transaction_id'];
        $this->_ta  = $params['affiliation'];
        $this->_tr  = number_format( $params['revenue'], 2 );
        $this->_ts  = number_format( $params['shipping'], 2 );
        $this->_tt  = number_format( $params['tax'], 2 );       
    }

    public function addItem( TrackingItem $item ) {
        $this->_items[]     = $item;
        $this->_urls[]  = $item->getUrl();
    }

    public function getItems() {
        return $this->_items;   
    }

    public function getUrls() {
        return $this->_urls;    
    }

    public function send() {

        if( ! empty( $this->_urls ) ) {
            foreach( $this->_urls as $url ) {

                $ch = curl_init();              
                curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );  
                curl_setopt( $ch, CURLOPT_URL, 'http://www.google-analytics.com/collect' ); 
                curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
                curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false ); 
                curl_setopt( $ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY ); 
                curl_setopt( $ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.2; .NET CLR 2.0.50727)' );
                curl_setopt( $ch, CURLOPT_POSTFIELDS, $url ); 
                curl_exec( $ch );
                curl_close( $ch );

            }

        }

    }

}

TrackingItem 类

class TrackingItem {

    // The type of hit.
    protected $_t = 'item';

    // Specifies the item name.
    protected $_in;

    // Specifies the price for a single item / unit.
    protected $_ip = 0;

    // Specifies the number of items purchased.
    protected $_iq = 1;

    // Specifies the SKU or item code.
    protected $_ic;


    public function __construct( $name, $price, $qty, $sku ) {
        $this->_in  = $name;
        $this->_ip  = number_format( $price, 2 );
        $this->_iq  = $qty;
        $this->_ic  = $sku;
    }

    public function getName() {
        return $this->_in;  
    }

    public function getUrl() {
        $url = 'v=' . $this->_v . '&tid=' . $this->_tid . '&cid=' . $this->cid . '&t=' . $this->_t . 
        '&ti=' . $this->_ti . '&in=' . $this->_in . '&ip=' . $this-_ip . '&iq=' . $this->_iq . '&ic=' . $this->_ic; 
    }

}

创建对象

// Create tracking object
$tracking = new EcommerceTracking( array(
    'ua'                => 'UA-XXXX',
    'client_id'         => '10.0101.010.0101',
    'transaction_id'    => 123,
    'affiliation'       => 'Shop X',
    'revenue'           => 100,
    'shipping'          => 20,
    'tax'               => 20,
));

// Add tracking items
foreach( $items as $item ) {
    $tracking->addItem( new TrackingItem( 'Name', 34, 1, 'SKU001' ) );
}

所以我不想再次为该项目设置“ua”、“client_id”、“transaction_id”和“affiliation”。我希望该项目从跟踪对象继承它。

4

0 回答 0