0

PHP で Google e コマース追跡クラスに取り組んでいます。このクラスの目的は、cURL で Google アナリティクスに送信する URL を作成することです。

最初に、次のようなパラメーターを使用してトランザクション (url) を作成します。

  • クライアントID
  • transaction_id
  • 所属

次に、トランザクション アイテム (商品はショッピング カート) を追加します。

項目が上記の最初の 4 つのパラメーターを継承することを望みます。そのため、毎回再設定する必要はありません。

ecommerceTracking クラス

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