PDA

View Full Version : How to track PHP sites, pages within frames, ASP, JavaScript



bahattab
21-06-2008, 02:12 PM
Installation details


How long does it take to see report data after adding the tracking code? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55541&topic=10981)
How do I track a new website? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55522&topic=10981)
What is _trackPageview and how can it help me? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55597&topic=10981)
How can I obtain tracking code for secure pages? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55483&topic=10981)
How do I track dynamic sites? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55504&topic=10981)
How often is the data in the reports updated? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55544&topic=10981)
Can I upload existing log or reporting data into my Google Analytics account? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55472&topic=10981)
Can I host the ga.js file locally? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55466&topic=10981)
What customizations are possible with the Analytics tracking code? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55585&topic=10981)
How can I use Google Analytics with my Content_Management System (CMS)? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55484&topic=10981)
How do I add the tracking code in Google Page Creator? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55487&topic=10981)
How do I run Google Analytics on my intranet? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55510&topic=10981)
How do I install the tracking code on an ASP site? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55505&topic=10981)
What is _link() and how can it help me? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55592&topic=10981)
Tracking Custom Search Engine results in Google Analytics (http://www.google.com/support/googleanalytics/bin/answer.py?answer=98222&topic=10981)
Which version of the tracking code am I using? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=75129&topic=10981)
What do the different Website Profile tracking status messages mean? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=91502&topic=10981)
Google Analytics Tracking Code Migration Guide (http://www.google.com/support/googleanalytics/bin/answer.py?answer=76305&topic=10981)


Tracking domains and subdomains

How do I use Google Analytics to track a 3rd-party shopping cart? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55532&topic=10982)
How do I track all of the subdomains for my site in one profile? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55524&topic=10982)
How do I install the tracking code if my site spans multiple domains? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55503&topic=10982)
How do I track all of the subdomains for my site in separate profiles? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55525&topic=10982)
I do not own my website domain. How do I set up Google Analytics? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55549&topic=10982)
My site has multiple domain names. How do I set up tracking? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55570&topic=10982)
How do I track a site that uses domain aliases? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55523&topic=10982)


Troubleshooting tracking

How can I confirm that I've entered the tracking code correctly on my pages? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55480&topic=10983)
Why isn't Google Analytics tracking my website? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=57053&topic=10983)
Why is Google Analytics not tracking some of the pages on my website? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55615&topic=10983)
Can I use both Google Analytics and Urchin at the same time? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55474&topic=10983)
How are redirects handled by Analytics? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55477&topic=10983)
What should I know about using Analytics with framed sites? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55598&topic=10983)
I'm having trouble creating a profile for my secure website. (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55552&topic=10983)
Can Analytics track my MySpace account? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=86258&topic=10983)
Why do my old profiles show the new code? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=76276&topic=10983)
Where can I find my tracking code and account number? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=81977&topic=10983)
My Blogger account stopped collecting data (http://www.google.com/support/googleanalytics/bin/answer.py?answer=81976&topic=10983)


Tracking directories

How do I track unique areas within my website separately? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55530&topic=11127)





How to track PHP sites, pages within frames, ASP, JavaScript


Note: This article is for the latest version of the tracking code. If you are using the older version, please read the following article (http://www.google.com/support/googleanalytics/bin/answer.py?answer=74959).

To determine which tracking code you are using, please see Which version of the tracking code am I using? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=75129) If your website pages are created dynamically with a technology such as PHP or ASP, you can include the tracking code in a separate file that gets included as part of the overall page template. The following example illustrates how you might do this using PHP, but the solution applies equally to any similar website technology (e.g. ASP or JSP). To track dynamic websites, please follow the steps below.
1. In a separate file, include the standard tracking code. For example, you might have a file called 'analyticsTracking.php' that contains the following:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._initData();
pageTracker._trackPageview();
</script> <?php
// End Analytics tracking code
?>
2. In the standard page template for your website, include the tracking code file before the closing </body> tag. Again, please note that this very simple example illustrates one way you might track dynamic websites using PHP, but the steps may be slightly modified depending on your particular website technology and the way your website pages are generated.

<html>
<?php include ("header.php"); ?>
<body>

<?php include ("sideNav.php"); ?>
<?php include ("main.php"); ?>
<?php include ("analyticsTracking.php"); ?>
</body>
</html>

Track a Secure Site (http://www.google.com/support/googleanalytics/bin/static.py?page=troubleshooter.cs&problem=tracking&selected=tracking_secure&ctx=tracking_tracking_secure_55483&aw_referral=)



Note: This article is for the latest version of the tracking code. If you are using the older version, please read the following article (http://www.google.com/support/googleanalytics/bin/answer.py?answer=74953).

To determine which tracking code you are using, please see Which version of the tracking code am I using? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=75129) The Google Analytics tracking code is now the same for both secure and non-secure websites. The new tracking code detects the protocol of the page being tracked and matches the security of your web page automatically. Pages with URLs beginning with http:// will download the non-secure version at http://www.google-analytics.com/ga.js and pages with URLs beginning with https:// will load the secure version at https://ssl.google-analytics.com/ga.js. No modification of the tracking code on secure pages is required.
Although the previous version of the tracking code (urchin.js) required you to copy/paste special tracking code into your secure pages, this is no longer necessary if you are tracking your site with ga.js. You can use the same tracking code on both secure (https://) and non-secure (http://) pages.
The new tracking code looks like this:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._initData();
pageTracker._trackPageview();
</script>


Pages Within Frames (http://www.google.com/support/googleanalytics/bin/static.py?page=troubleshooter.cs&problem=tracking&selected=tracking_frames&ctx=tracking_tracking_frames_55598&aw_referral=)



Installing the GA tracking code on a site that uses frames requires some careful consideration to ensure that your traffic is tracked accurately, and that the referral information is retained.


Where should I place my tracking code? (http://www.google.com/support/googleanalytics/bin/static.py?page=troubleshooter.cs&problem=tracking&selected=tracking_frames&ctx=tracking_tracking_frames_55598&aw_referral=#1)
Why isn't my CPC information appearing? (http://www.google.com/support/googleanalytics/bin/static.py?page=troubleshooter.cs&problem=tracking&selected=tracking_frames&ctx=tracking_tracking_frames_55598&aw_referral=#2)
Why aren't referrals showing up? (http://www.google.com/support/googleanalytics/bin/static.py?page=troubleshooter.cs&problem=tracking&selected=tracking_frames&ctx=tracking_tracking_frames_55598&aw_referral=#3)
Is my traffic being inflated? (http://www.google.com/support/googleanalytics/bin/static.py?page=troubleshooter.cs&problem=tracking&selected=tracking_frames&ctx=tracking_tracking_frames_55598&aw_referral=#4)

1. Where should I place my tracking code?
We recommend that a site which uses frames include the tracking code in the <head> section of the frameset page, as well as directly before the </body> tag of each child frame that you want to track. (The tracking code goes into the head section of the frameset page, as these documents don't contain a body section.) If you place your tracking code within a <noframe> tag thinking that it's equivalent to the body, the code will not run and no data will be recorded. 2. Why isn't my CPC information appearing even though I've enabled auto-tagging?
The landing page URL of your ads is the frameset document, not the child frames within that document. If you haven't added tracking code to the <head> section of your frameset, Analytics won't track that page and your campaign parameters will be lost. 3. Why aren't referrals showing up?
If all frames on the site reside on the same domain, then referral information can be properly recorded as long as you've installed the tracking code on the first page users visit on the site. If frames on the site reside on different domains, the referral information is likely to be inaccurate, since one frame may be recorded as the referring source of another, instead of a previous site being recorded as the referring source.
In either case, the Navigation Summary and Site Overlay reports for sites with frames is not likely to provide useful information, since every frame with tracking code on it will be recorded as a separate pageview. Additionally, the order of these pageviews will depend on the order in which the frames are loaded instead of the actual navigation path taken by your visitors.
4. Is my traffic being inflated?
Users may experience what they consider to be inflated traffic since every framed page, as well as the frameset, will be tracked individually. For example, if a frameset with tracking code has three frames, with tracking code installed in each frame, a single visit to the frameset URL will result in four pageviews. Users may choose to track only some of the framed pages, or only the frameset itself. Your implementation will depend on what data you're interested in collecting.









JavaScript Events (http://www.google.com/support/googleanalytics/bin/static.py?page=troubleshooter.cs&problem=tracking&selected=tracking_javascript&ctx=tracking_tracking_javascript_55521&aw_referral=)





Note: This article is for the latest version of the tracking code. If you are using the older version, please read the following article (http://www.google.com/support/googleanalytics/bin/answer.py?answer=74980).

To determine which tracking code you are using, please see Which version of the tracking code am I using? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=75129) By using the _trackPageviewfunction, you can assign a page filename to any Javascript event.
Important: if your pages include a call to _trackPageview, _setAllowLinker _trackTrans(), your Analytics tracking code must be placed in your HTML code above any of these calls. In these cases the tracking code can be placed anywhere between the opening <body> tag and the JavaScript call.
To track an event, call _trackPageview() with an argument specifying a name for the event. For example:
pageTracker._trackPageview("/homepage/link1");
will cause each occurrence of the calling event to be logged as though it were a pageview for the URL /homepage/link1 . The argument must begin with a forward slash and be in quotes. The event names may be organized into any directory style structure you wish.
For example, if you wish to organize events by page > type of event, you might organize a hierarchy along these lines:


'/homepage/links/link1'
'/homepage/radiobuttons/button1'
'/contactform/rollovers/image1'

HTML Code Examples
The following illustrates how to log an onClick event:

<a href="javascript:void(0);"onClick="javascript:pageTracker._trackPageview('/folder/file');" > Or, to log a rollover event:

<a href="javascript:void(0);" onMouseOver="javascript:pageTracker._trackPageview('/folder/file');" > To verify that pageTracker is being called correctly, you can check your Top Content report 24-48 hours after the updated tracking code has been executed. You should be able to see the assigned pagename in your report.





Track Downloadable Files (http://www.google.com/support/googleanalytics/bin/static.py?page=troubleshooter.cs&problem=tracking&selected=tracking_downloads&ctx=tracking_tracking_downloads_55529&aw_referral=)



Note: This article is for the latest version of the tracking code. If you are using the older version, please read the following article (http://www.google.com/support/googleanalytics/bin/answer.py?answer=74985).

To determine which tracking code you are using, please see Which version of the tracking code am I using? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=75129)
Google Analytics provides an easy way to track clicks on links that lead to file downloads. Because these links do not lead to a page on your site containing the tracking code, you'll need to tag the link itself with the _trackPageview() JavaScript if you would like to track these downloads. This piece of JavaScript assigns a pageview to any click on a link - the pageview is attributed to the filename you specify.
For example, to log every click on a particular link to www.example.com/files/map.pdf (http://www.example.com/files/map.pdf) as a pageview for /downloads/map you would add the following attribute to the link's <a> tag:

<a href="http://www.example.com/files/map.pdf" onClick="javascript: pageTracker._trackPageview('/downloads/map'); "> Important: if your pages include a call to _trackPageview, _setAllowLinker _trackTrans(), your Analytics tracking code must be placed in your HTML code above any of these calls. In these cases the tracking code can be placed anywhere between the opening <body> tag and the JavaScript call.
To verify that _trackPageviewis being called correctly, you can check your Top Content report 24-48 hours after the updated tracking code has been executed. You should be able to see the assigned pagename in your report.





Outbound Links (http://www.google.com/support/googleanalytics/bin/static.py?page=troubleshooter.cs&problem=tracking&selected=tracking_outbound&ctx=tracking_tracking_outbound_55527&aw_referral=)



Note: This article is for the latest version of the tracking code. If you are using the older version, please read the following article (http://www.google.com/support/googleanalytics/bin/answer.py?answer=74983).

To determine which tracking code you are using, please see Which version of the tracking code am I using? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=75129) Google Analytics provides an easy way to track clicks on links that lead away from your site. Because these links do not lead to a page on your site containing the UTM JavaScript, you will need to tag the link itself. This piece of JavaScript assigns a pageview to any click on a link - the pageview is attributed to the filename you specify.
For example, to log every click on a particular link to www.example.com (http://www.example.com) as a pageview for "/outgoing/example_com" you would add the following attribute to the link's tag:

<a href="http://www.example.com" onClick="javascript: pageTracker._trackPageview('/outgoing/example.com');"> It is a good idea to log all of your outbound links into a logical directory structure as shown in the example. This way, you will be able to easily identify what pages visitors clicked on to leave your site.
Important: if your pages include a call to _trackPageview, _setAllowLinker _trackTrans(), your Analytics tracking code must be placed in your HTML code above any of these calls. In these cases the tracking code can be placed anywhere between the opening <body> tag and the JavaScript call.
To verify that _trackPageviewis being called correctly, you can check your Top Content report 24-48 hours after the updated tracking code has been executed. You should be able to see the assigned pagename in your report.




E-Commerce Transactions (http://www.google.com/support/googleanalytics/bin/static.py?page=troubleshooter.cs&problem=tracking&selected=tracking_ecommerce&ctx=tracking_tracking_ecommerce_55528&aw_referral=)


Note: This article is for the latest version of the tracking code. If you are using the older version, please read the following article (http://www.google.com/support/googleanalytics/bin/answer.py?answer=74984).

To determine which tracking code you are using, please see Which version of the tracking code am I using? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=75129) (If you're looking for information on tracking visitor movement through your checkout pages, please read How do I use Google Analytics to track a 3rd-party shopping cart? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=55532))
Enabling E-Commerce Reporting in the Profile
The first step of tracking e-commerce transactions is to enable e-commerce reporting for your website's profile. To enable e-commerce reporting, please follow these steps:


Log in to your account.
Click Edit next to the profile you'd like to enable.
On the Profile Settings page, click edit next to Main Website Profile Information.
Change the E-Commerce Website radio button from No to Yes.

Receipt Page Format
The second step is to ensure that the tracking code is included in the receipt page in the standard fashion. This may be done using a server side include, or other template driver, or can simply be hand-coded into your HTML code:



<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._initData();
pageTracker._trackPageview();
</script>
Next, somewhere in the receipt below the tracking code, call the _addTrans() and _addItem() functions. Begin with a call to _addTrans() to establish a transaction. _addTrans() takes the following arguments: Order ID,Affiliation, Total, Tax, Shipping, City, State, Country. For each item that the visitor purchases, call _addItem(). _addItem() takes the following arguments: Order ID, SKU or Code, Product Name, Category, Price, Quantity.
Here is a complete example:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>

<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-XXXXX-1");
pageTracker._initData();
pageTracker._trackPageview();

pageTracker._addTrans(
"1234", // Order ID
"Mountain View", // Affiliation
"11.99", // Total
"1.29", // Tax
"5", // Shipping
"San Jose", // City
"California", // State
"USA" // Country
);

pageTracker._addItem(
"1234", // Order ID
"DD44", // SKU
"T-Shirt", // Product Name
"Green Medium", // Category
"11.99", // Price
"1" // Quantity
);

pageTracker._trackTrans();
</script>
Flash Events (http://www.google.com/support/googleanalytics/bin/static.py?page=troubleshooter.cs&problem=tracking&selected=tracking_flash&ctx=tracking_tracking_flash_55520&aw_referral=)



Note: This article is for the latest version of the tracking code. If you are using the older version, please read the following article (http://www.google.com/support/googleanalytics/bin/answer.py?answer=74979).

To determine which tracking code you are using, please see Which version of the tracking code am I using? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=75129) Google Analytics lets you track any browser based event, including Flash and JavaScript events by using the _trackPageview function, you can assign a page filename to any Flash action, and enter that filename into the appropriate goal or funnel step. Important: Please note that your analytics tracking code and calls to _gat._getTracker and _initData must be placed on the page above the call to _trackPageview.

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._initData();
pageTracker._trackPageview();
</script> To track an event, call _trackPageview() with an argument specifying a name for the event. For example, calling:
pageTracker._trackPageview("/purchase_funnel/page1.html");
will cause each occurrence of the the calling Flash event to be logged as though it were a pageview under the name /purchase_funnel/page1.html . The argument must begin with a forward slash and be in quotes. The event names may be organized into any directory style structure you wish. The path/filename argument to _trackPageview need not represent an actual URL on your website.
Flash Code Examples

on (release) {
// Track with no action
getURL("javascript:pageTracker._trackPageview('/folder/file.html');");
}
on (release) {
//Track with action
getURL("javascript:pageTracker._trackPageview('/folder/file.html');");
_root.gotoAndPlay(3);
myVar = "Flash Track Test";
}
onClipEvent (enterFrame) {
getURL("javascript:pageTracker._trackPageview('/folder/file.html');");
}
To verify that _trackPageview is being called correctly, you can check your Top Content report 24-48 hours after the updated tracking code has been executed. You should be able to see the assigned pagename in your report.




ASP Pages (http://www.google.com/support/googleanalytics/bin/static.py?page=troubleshooter.cs&problem=tracking&selected=tracking_asp&ctx=tracking_tracking_asp_55505&aw_referral=)



Note: This article is for the latest version of the tracking code. If you are using the older version, please read the following article (http://www.google.com/support/googleanalytics/bin/answer.py?answer=74960).

To determine which tracking code you are using, please see Which version of the tracking code am I using? (http://www.google.com/support/googleanalytics/bin/answer.py?answer=75129) To add the tracking code to your ASP site, add the following line to each template page for your site before the closing tag: <!--#include file="analytics.asp"--> Then, upload a file called analytics.asp to your site. This file should contain your tracking code:

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-xxxxxx-x");
pageTracker._initData();
pageTracker._trackPageview();
</script>










Google Analytics (http://www.google.com/support/googleanalytics/bin/static.py?page=troubleshooter.cs&problem=tracking&selected=tracking_php&ctx=tracking_tracking_php_55504&aw_referral=)