Skip to content

Instantly share code, notes, and snippets.

@stakahashi
Created September 28, 2015 16:39
Show Gist options
  • Save stakahashi/394aa8cb6699676edaec to your computer and use it in GitHub Desktop.
Save stakahashi/394aa8cb6699676edaec to your computer and use it in GitHub Desktop.
PHPでGoogleAnalyticsにトラッキング情報を送信する ref: http://qiita.com/shunsuke-takahashi/items/53703f6b943fb32d2dd6
php composer.phar install theiconic/php-ga-measurement-protocol
<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-xx', 'auto');
ga('send', 'pageview');
</script>
use TheIconic\Tracking\GoogleAnalytics\Analytics;
$analytics = new Analytics(true);
// ga('create', 'UA-xxxxxxxx-xx', 'auto');
$analytics->setProtocolVersion('1')
->setTrackingId('UA-xxxxxxxx-xx')
->setDocumentPath('/mypage')
->setClientId('12345678');
// ga('send', 'pageview');
$analytics->sendPageview();
<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-xx', 'auto');
ga('event', 'category', 'action', 'label', 'value');
</script>
use TheIconic\Tracking\GoogleAnalytics\Analytics;
$analytics = new Analytics(true);
$analytics->setProtocolVersion('1')
->setTrackingId('UA-xxxxxxxx-xx')
->setDocumentPath('/mypage')
->setClientId('12345678');
// ga('event', 'category', 'action', 'label', 'value');
$analytics->setEventCategory('category')
->setEventAction('action')
->setEventLabel('label')
->setEventValue('value')
->sendEvent();
<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-xx', 'auto');
// @see https://developers.google.com/analytics/devguides/collection/analyticsjs/ecommerce
ga('ecommerce:addTransaction', {
'id': '1234', // Transaction ID. Required.
'affiliation': 'Acme Clothing', // Affiliation or store name.
'revenue': '11.99', // Grand Total.
'shipping': '5', // Shipping.
'tax': '1.29' // Tax.
});
ga('ecommerce:addItem', {
'id': '1234', // Transaction ID. Required.
'name': 'Fluffy Pink Bunnies', // Product name. Required.
'sku': 'DD23444', // SKU/code.
'category': 'Party Toys', // Category or variation.
'price': '11.99', // Unit price.
'quantity': '1' // Quantity.
});
ga('ecommerce:send');
</script>
use TheIconic\Tracking\GoogleAnalytics\Analytics;
$analytics = new Analytics(true);
$analytics->setProtocolVersion('1')
->setTrackingId('UA-xxxxxxxx-xx')
->setDocumentPath('/mypage')
->setClientId('12345678');
// ecommerce:addTransaction
$analytics->setTransactionId(1234)
->setAffiliation('Acme Clothing')
->setRevenue('11.99')
->setShipping('5')
->setTax('1.29');
// ecommerce:addItem
$item = [
'sku' => 'DD23444',
'name' => 'Fluffy Pink Bunnies',
'category' => 'Party Toys',
'price' => '11.99',
'quantity' => '1',
];
$analytics->addProduct($_item);
// ecommerce:send
$analytics->setProductActionToPurchase();
ga(function(tracker) {
var clientId = tracker.get('clientId');
});
ga('create', 'UA-XXXX-Y', 'auto', {
'clientId': clientId // Value is retrieved from method above.
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment