如何测试洗牌程序

如何测试洗牌程序

coolshell.cn

|
Nov 30th -0001


我希望本文有助于你了解测试软件是一件很重要也是一件不简单的事。

我们有一个程序,叫ShuffleArray(),是用来洗牌的,我见过N多千变万化的ShuffleArray(),但是似乎从来没人去想过怎么去测试这个算法。所以,我在面试中我经常会问应聘者如何测试ShuffleArray(),没想到这个问题居然难倒了很多有多年编程经验的人。对于这类的问题,其实,测试程序可能比算法更难写,代码更多。而这个问题正好可以加强一下我在《我们需要专职的QA吗?
》中我所推崇的——开发人员更适合做测试的观点。

我们先来看几个算法(第一个用递归二分随机抽牌,第二个比较偷机取巧,第三个比较通俗易懂

递归二分随机抽牌

有一次是有一个朋友做了一个网页版的扑克游戏,他用到的算法就是想模拟平时我们玩牌时用手洗牌的方式,是用递归+二分法,我说这个程序恐怕不对吧。他觉得挺对的,说测试了没有问题。他的程序大致如下(原来的是用Javascript写的,我在这里凭记忆用C复现一下):

//递归二分方法
const size_t MAXLEN = 10;
const char TestArr[MAXLEN] = {'A','B','C','D','E','F','G','H','I','J'};

static char RecurArr[MAXLEN]={0};
static int cnt = 0;
void ShuffleArray_Recursive_Tmp(char* arr, int len)
{
    if(cnt > MAXLEN || len <=0){
        return;
    }

    int pos = rand() % len;
    RecurArr[cnt++] = arr[pos];
    if (len==1) return;
    ShuffleArray_Recursive_Tmp(arr, pos);
    ShuffleArray_Recursive_Tmp(arr+pos+1, len-pos-1);
}

void ShuffleArray_Recursive(char* arr, int len)
{
    memset(RecurArr, 0, sizeof(RecurArr));
    cnt=0;
    ShuffleArray_Recursive_Tmp(arr, len);
    memcpy(arr, RecurArr, len);
}

void main()
{
    char temp[MAXLEN]={0};
    for(int i=0; i<5; i++) {
        strncpy(temp, TestArr, MAXLEN);
        ShuffleArray_Recursive((char*)temp, MAXLEN);
    }
}

随便测试几次,还真像那么回事:

第一次:D C A B H E G F I J
第二次:A G D B C E F J H I
第三次:A B H F C E D G I J
第四次:J I F B A D C E H G
第五次:F B A D C E H G I J

快排Hack法

让我们再看一个hack 快排的洗牌程序(只看算法,省去别的代码):

int compare( const void *a, const void *b )
{
    return rand()%3-1;
}

void ShuffleArray_Sort(char* arr, int len)
{
    qsort( (void *)arr, (size_t)len, sizeof(char), compare );
}

运行个几次,感觉得还像那么回事:

第一次:H C D J F E A G B I
第二次:B F J D C E I H G A
第三次:C G D E J F B I A H
第四次:H C B J D F G E I A
第五次:D B C F E A I H G J

看不出有什么破绽。

大多数人的实现

下面这个算法是大多数人的实现,就是for循环一次,然后随机交换两个数

void ShuffleArray_General(char* arr, int len)
{
    const int suff_time = len;
    for(int idx=0; idx

跑起来也还不错,洗得挺好的。

第一次:G F C D A J B I H E
第二次:D G J F E I A H C B
第三次:C J E F A D G B H I
第四次:H D C F A E B J I G
第五次:E A J F B I H G D C

但是上述三个算法哪个的效果更好?好像都是对的。一般的QA或是程序员很有可能就这样把这个功能Pass了
。但是事情并没有那么简单……

如何测试

在做测试之前,我们还需要了解一下一个基本知识——PC机上是做不出真随机数的,只能做出伪随机数。真随机数需要硬件支持
。但是不是这样我们就无法测试了呢,不是的。我们依然可以测试。

我们知道,洗牌洗得好不好,主要是看是不是够随机。那么如何测试随机性呢?

试想,我们有个随机函数rand()返回1到10中的一个数,如果够随机的话,每个数返回的概率都应该是一样的,也就是说每个数都应该有10分之1的概率会被返回。

一到概率问题,我们只有一个方法来做测试,那就是用统计的方式。也就是说,你调用rand()函数100次,其中,每个数出现的次数大约都在10次左右。(注意:我用了左右,这说明概率并不是很准确的)不应该有一个数出现了15次以上,另一个在5次以下,要是这样的话,这个函数就是错的。

举一反三,测试洗牌程序也一样,需要通过概率的方式来做统计,是不是每张牌出现在第一个位置的次数都是差不多的。

于是,这样一来上面的程序就可以很容易做测试了。

下面是测试结果(测试样本1000次——列是每个位置出现的次数,行是各个字符的统计
,出现概率应该是1/10,也就是100次):

递归随机抽牌的方法

很明显,这个洗牌程序太有问题。算法是错的!

     1    2    3    4    5    6    7    8    9    10
----------------------------------------------------
A | 101  283  317  208   65   23    3    0    0    0
B | 101  191  273  239  127   54   12    2    1    0
C | 103  167  141  204  229  115   32    7    2    0
D | 103  103   87  128  242  195  112   26    3    1
E | 104   83   62   67  116  222  228   93   22    3
F |  91   58   34   60   69  141  234  241   65    7
G |  93   43   35   19   44  102  174  274  185   31
H |  94   28   27   27   46   68   94  173  310  133
I | 119   27   11   30   28   49   64   96  262  314
J |  91   17   13   18   34   31   47   88  150  511

快排Hack法

看看对角线(从左上到右下)上的数据,很离谱!所以,这个算法也是错的。

      1    2    3    4    5    6    7    8    9    10
-----------------------------------------------------
A |   74  108  123  102   93  198   40   37   52  173
B |  261  170  114   70   49   28   37   76  116   79
C |  112  164  168  117   71   37   62   96  116   57
D |   93   91  119  221  103   66   91   98   78   40
E |   62   60   82   90  290  112   95   98   71   40
F |   46   60   63   76   81  318   56   42   70  188
G |   72   57   68   77   83   39  400  105   55   44
H |   99   79   70   73   87   34  124  317   78   39
I |  127  112  102   90   81   24   57   83  248   76
J |   54   99   91   84   62  144   38   48  116  264

大多数人的算法

我们再来看看大多数人的算法。还是对角线上的数据有问题,所以,还是错的。

      1    2    3    4    5    6    7    8    9    10
-----------------------------------------------------
A |  178   98   92   82  101   85   79  105   87   93
B |   88  205   90   94   77   84   93   86  106   77
C |   93   99  185   96   83   87   98   88   82   89
D |  105   85   89  190   92   94  105   73   80   87
E |   97   74   85   88  204   91   80   90  100   91
F |   85   84   90   91   96  178   90   91  105   90
G |   81   84   84  104  102  105  197   75   79   89
H |   84   99  107   86   82   78   92  205   79   88
I |  102   72   88   94   87  103   94   92  187   81
J |   87  100   90   75   76   95   72   95   95  215

正确的算法

下面,我们来看看性能高且正确的算法—— Fisher_Yates算法

void ShuffleArray_Fisher_Yates(char* arr, int len)
{
    int i = len, j;
    char temp;

    if ( i == 0 ) return;
    while ( --i ) {
        j = rand() % (i+1);
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

这个算法不难理解,看看测试效果(效果明显比前面的要好):

      1    2    3    4    5    6    7    8    9    10
-----------------------------------------------------
A |  107   98   83  115   89  103  105   99   94  107
B |   91  106   90  102   88  100  102   97  112  112
C |  100  107   99  108  101   99   86   99  101  100
D |   96   85  108  101  117  103  102   96  108   84
E |  106   89  102   86   88  107  114  109  100   99
F |  109   96   87   94   98  102  109  101   92  102
G |   94   95  119  110   97  112   89  101   89   94
H |   93  102  102  103  100   89  107  105  101   98
I |   99  110  111  101  102   79  103   89  104  102
J |  105  112   99   99  108  106   95   95   99   82

但是我们可以看到还是不完美。因为我们使用的rand()是伪随机数,不过已经很不错的。最大的误差在20%左右。

我们再来看看洗牌100万次的统计值,你会看到误差在6%以内了。这个对于伪随机数生成的程序已经很不错了。

      1       2     3       4      5      6      7      8     9      10
-------------------------------------------------------------------------
A | 100095  99939 100451  99647  99321 100189 100284  99565 100525  99984
B |  99659 100394  99699 100436  99989 100401  99502 100125 100082  99713
C |  99938  99978 100384 100413 100045  99866  99945 100025  99388 100018
D |  99972  99954  99751 100112 100503  99461  99932  99881 100223 100211
E | 100041 100086  99966  99441 100401  99958  99997 100159  99884 100067
F | 100491 100294 100164 100321  99902  99819  99449 100130  99623  99807
G |  99822  99636  99924 100172  99738 100567 100427  99871 100125  99718
H |  99445 100328  99720  99922 100075  99804 100127  99851 100526 100202
I | 100269 100001  99542  99835 100070  99894 100229 100181  99718 100261
J | 100268  99390 100399  99701  99956 100041 100108 100212  99906 100019

如何写测试案例

测试程序其实很容易写了。就是,设置一个样本大小,做一下统计,然后计算一下误差值是否在可以容忍的范围内。比如:

  • 样本:100万次
  • 最大误差:10%以内
  • 平均误差:5%以内 (或者:90%以上的误差要小于5%)

注意

其实,以上的测试只是测试了牌在各个位置的概率。这个还不足够好。因为还可能会现在有Patten的情况。如:每次洗牌出来的都是一个循环顺序数组。这完全可以满足我上面的测试条件。但是那明显是错的。所以,还需要统计每种排列的出现的次数
,看看是不是均匀。但是,如果这些排列又是以某种规律出现的呢
?看来,这没完没了了。

测试的确是一个很重要,并不简单的事情。谢谢所有参与讨论的人。

附录

之前忘贴了一个模拟我们玩牌洗牌的算法,现补充如下:

void ShuffleArray_Manual(char* arr, int len)
{
    int mid = len / 2;

    for (int n=0; n<5; n++){

        //两手洗牌
        for (int i=1; i<5; j++) {
            int start= rand() % (len-1) + 1;
            int numCards= rand()% (len/2) + 1;

            if (start + numCards > len ){
                numCards = len - start;
            }

            memset(buf, 0, len);
            strncpy(buf, arr, start);
            strncpy(arr, arr+start, numCards);
            strncpy(arr+numCards, buf, start);
        }
        free(buf);

    }
}

我们来看看测试结果:(10万次)效果更好一些,误差在2%以内了。

      1       2     3       4      5      6      7      8     9      10
-------------------------------------------------------------------------
A |  10002   9998   9924  10006  10048  10200   9939   9812  10080   9991
B |   9939   9962  10118  10007   9974  10037  10149  10052   9761  10001
C |  10054  10100  10050   9961   9856   9996   9853  10016   9928  10186
D |   9851   9939   9852  10076  10208  10003   9974  10052   9992  10053
E |  10009   9915  10050  10037   9923  10094  10078  10059   9880   9955
F |  10151  10115  10113   9919   9844   9896   9891   9904  10225   9942
G |  10001  10116  10097  10030  10061   9993   9891   9922   9889  10000
H |  10075  10033   9866   9857  10170   9854  10062  10078  10056   9949
I |  10045   9864   9879  10066   9930   9919  10085  10104  10095  10013
J |   9873   9958  10051  10041   9986  10008  10078  10001  10094   9910

(全文完)


Original Page: http://coolshell.cn/articles/8593.html

Shared from Pocket

为什么待办事项清单不管用? : 褪墨

为什么待办事项清单不管用? : 褪墨

mifengtd.cn

|
Nov 30th -0001


本文原文来自 Harvard Business Review
,由 @换装迷宫tayy 翻译。


停止制作你的待办事项清单
吧。它们只会让你感觉失败和受挫。想想你正在管理的那些待办清单:有多少事项从年初起就已经在那儿了?你是不是每次浏览你的清单都是为了挑出那些两分钟就能做完的事项?有多少事项根本就不是事项,而是更复杂、需要明确计划的项目

导致你的待办事项清单失效的主要原因来自五个方面:

选择悖论

巴里·施华兹(Barry Schwartz)和希娜·亚格尔(Sheena Lyengar)探索了因为选择太多而产生的问题。施华兹教授点出,随着我们可选择的选项数量增加:单层还是双层?厚面还是平面?芦荟味道的还是超柔软的?我们的负面情绪也在增加
。因为我们对机会成本的感觉增加了。在补充研究中,亚格尔教授证实了我们的大脑在崩溃之前只能处理大约七个选项
。也就是说,让我们从少一些的选项中做出选择并实施要更容易一些。看着待办事项清单上的58条任务,你要么会不知所措,要么会自动进入默认模式:花一个小时查邮件而不是做真正的工作。

不均的复杂性

如果你的清单上有一些工作只需要3分钟完成,有的却需要33分钟,那根据想要尽快从清单上划掉某事项的心理回报和多巴胺释放原理,你一定会把精力集中在那些耗时短的事项上。这就是说,有些事项——比如校对一本135的新员工福利手册——会等上很长很长一段时间。

不均的优先性

如果你的清单事项分为不同的优先等级
,那么你会倾向于先做A等级,并把C等级事项向后排……直到它们也成为A等级。但是你愿意在你的汽车维护在C等级的时候就把它做好,还是等到它半夜三点停在离家175英里以外的沙漠边上成为A等级的时候呢?

缺少情境

待办事项清单没有给你提供足够的情境,以便帮助你决定你该做什么。所有的任务在纸上时看上去都一样——一行三四个字。但是这并没有捕捉或显示出你需要的那些重要信息:任务需要多长时间?以及其推论:你有多少时间?如果你不能回答这些问题,那你就不能明智地决定你应该什么。

缺少承诺装置

待办事项清单不能阻止你选择最舒适的任务而不是最重要
的(通常也是最难的)任务,因为清单缺少能够将你锁定在一个你也许不会选择的任务中的“承诺装置”。中国的大将军韩信用地理环境作为承诺装置:他让他的士兵背对着河水与敌人作战,这样他们就不能逃跑(背水一战)。更现实的情况是:将设定好时间控制网络连接的软件来用作承诺装置。

一个解决无效的待办事项清单的方法被我称作“生活在你的日历中
”。意思是说,把你的任务从待办清单中拿掉,估计你需要花多少时间完成它,然后将其转移到你的日历中。(不要忘记留些时间处理你的邮件,并留出一些空余时间——一到两小时——每天去处理临时突发事件。)实质上,你就是在做一份工作的生产计划。


决定什么时间做什么工作
可以克服选择悖论、补偿工作固有的不均匀性、提供截止时间情境和其他承诺、提供一个(软性的)承诺装置,使你能够在对的时间做对的事情。

这是一个令人瞠目的练习:你也许会觉得困难——甚至不可能——去给每件事都安排一个时间。但是这是你的阵势生活。你只是简单地在用日历来描绘你的真实时间安排。不论你是不是让这个时间安排清晰可见,它就在那儿。归根结底,如果你会被一辆卡车碾过,你还是需要拿到卡车的车牌号。

将你的工作放在日历中能够让你更好地确定你是否可以(或是应该)对一个新的项目说yes。并且如果你说了yes,你能更好地确定你什么时候能够完成它。

你也许会想“我不可能告诉我的老板我在二月中之前完成不了这件事。”但是我必须告诉你你不得不说no。财务总监会在老板想搬进新的大楼或是想招募新员工但公司无法负担的时候告诉他不可以这么做——这是她责任的一部分。你也许会有同样的责任——去设置一个在规定时间内可能达到的期望值。

所以为了你自己,从今天起丢掉你的待办事项清单,开始生活在你的日历里吧!


Original Page: http://www.mifengtd.cn/articles/why-to-do-lists-dont-work.html

Shared from Pocket

iPhone HTTP Connection Debugging | Rain Blog

iPhone HTTP Connection Debugging

Nearly all iPhone applications take advantage of the device’s amazing internet connectivity. Whether its checking the balance of your bank account or using an interactive web application in Safari, there is a tangible level of connectedness. Wouldn’t it be great if you could watch, log, and edit the HTTP requests live as they happen from the comfort of your desktop? Not only is it possible, it is easier than you might think and doesn’t require jailbreaking your device! This article shows you how in just a few easy steps.

<soapbox>

May I take this opportunity to offer my highest recommendation for a wonderful piece of software called the Charles debugging proxy. The software allows you to view and edit HTTP requests that filter through its proxy server. It is secure, easy to install and configure, and runs on both Windows and OS X. Charles has become an essential tool in my web developer bag of tricks and I use it everyday.

You can download a free trial, but you won’t regret purchasing a license for only US $50.

http://www.charlesproxy.com

</soapbox>

This tutorial will focus on connecting iPhone applications with the Charles debugger

Prerequisites

  • A wireless LAN subnet to which your iPhone has access and is connected
  • A laptop or desktop machine on the same LAN subnet as your iPhone
  • Charles debugging proxy installed

There are two main areas of setup, the Charles proxy host, and the iPhone device.

Part 1 – Charles Proxy Host Setup

  1. Run Charles and verify that you have sufficiently enabled access through your system’s local firewall. Often this simply involves disabling the software firewall on your operating system. Charles needs to be listening on your LAN IP address and its default port is 8888. See your Operating System’s documentation for this specific procedure.
  2. Note your system’s IP address. In my case it is 10.45.200.127

Part 2 – iPhone Setup

  1. Download the charles SSL intermediate certificate

    http://www.charlesproxy.com/charles_ca_certificate.zip

  2. Extract the charles.cer file from the zip archive.

    At this point you have two ways you can install the charles.cer file on your device. You can either email it to yourself and open the attachment from the device. Or, you can post the file on a web server and request the certificate in Safari on the device. Either method will raise an Install prompt that looks like the one below:

  3. Configure your iPhone to use a proxy server

    This step involves going to the Settings application and into the Wi-Fi section. There you will hopefully see an option for the Wireless AP name you are connected to.

    Go to: Settings -> Wi-Fi -> “Your Wireless AP Name”

    Once there, scroll to the bottom of the window and you should see settings options to configure a proxy host as below:

    Enter your Charles proxy host address and port in the inputs provided

    NOTE: Once you are done using the Proxy, you will have to set your HTTP Proxy setting to “Off” for your phone to return to normal function.

  4. Test the configuration

    Open Safari and visit a website. You should see a prompt on your Charles machine to authorize the connection (this keeps unauthorized users from using your Proxy server):

    Once the connection has been authorized, voilá! You should now see your phone’s HTTP requests live in Charles. For even more detail, you can click on the “Sequence” view.

Several useful things you can do with Charles at this point:

  1. Save file requests to disk by right clicking and choosing “Save Response…”
  2. Edit and resend HTTP requests by clicking the “Edit” icon on the toolbar with a request selected
  3. Debug SSL connections
  4. Throttle and simulate 3G network speeds in order to view how your app may respond. See “Throttle Settings” under the Proxy menu option.

Take a look at the Charles Documentation for other tips and tricks:

http://www.charlesproxy.com/documentation/using-charles/

Enjoy, and happy hacking!!

ShareThis

[GUIDE] How to unbrick a totally dead MBL – Western Digital Community

[GUIDE] How to unbrick a totally dead MBL

[ Edited ]

07-12-2012 08:15 AM – last edited Tuesday

This guide allows you to debrick a MBL 1, 2, or 3tb drive using Windows, a bootable Live System Rescue CD and a 4gb or greater thumbdrive formatted to FAT32

Be aware that this will void your warranty and is only to be used as a last resort when you have no dashboard or SSH access

Your first step is to take the drive out of the case…

There’s only 4 latches holding the cover on (2 on each side). You just need a small flathead screwdriver and something thin like 2 credit cards to work the latches loose. Work one side first around the latches and when you get some seperation between the cover and the cage stick a credit card or something thin in there to keep the seperation then flip over to the other side. Start working around the latches on this side and when you get some seperation stick a credit card in to keep the seperation and flip back and forth as needed while continuing to work around the latches until you are able to seperate the latches from the clips. Once the cover starts sliding forward off the cage, slide the cover forward and off. Lift the clear plastic LED piece from it’s resting place in the cage and it’s resting place between the rubber flaps on the PCB attached to the hard drive. There are 4 rubber feet on metal brackets attached to the drive holding the drive in the cage. Push the drive up on the lift side as far as you can (it should JUST clear the cage) then pull it out at an angle. Unscrew the 4 screws on the pcb (1 is hidden under the rubber flap that holds the LED piece) and then slide the pcb straight back out of the drives sata connector. There is a thin piece of plastic between the pcb and the hard drive so make sure to get that back in there when you reassemble. At this point remove the metal brackets and metal shield so you can drop the hard drive in a SATA dock or inside your computers case.

 

 

All of these steps are done under Windows until you get to the point where I tell you to boot off the SystemRescueCd

If at this point you would like to pull your data off the MyBookLive for safe keeping, download Disk Internals Linux Reader for Windows http://www.diskinternals.com/linux-reader/ and install the program. Hook up the drive to your computer or a SATA dock and copy your data to another drive with enough space on your computer.

Download 7zip from  http://www.7-zip.org/download.html

Download SystemRescueCd from  http://www.sysresccd.org/Download

Download the debrick script from  http://www.mediafire.com/?g2xexkaaya34a9r

Download MybookLive firmware 02.11.09-053 from http://download.wdc.com/nas/apnc-021109-053-20120413.deb

For those wanting to debrick to the LATEST firmware

http://download.wdc.com/nas/apnc-023205-046-20120910.deb

Install 7zip and point it to your MyBookLive firmware "apnc-021109-053-20120413.deb" and double click and you will see "data.tar" where you will again double click and be presented with a "." Double click the "." then double click "cache volume"  then double click "upgrade" and then highlight  "rootfs.img" and hit extract.

Next burn the SystemRescueCd ISO to a blank CD

Use 7zip or WinRAR to extract the debricking script "debrick.sh" from the "debrick.rar" file you downloaded above

Copy "debrick.sh" and "rootfs.img" to your thumbdrive

Make sure your thumbdrive containing the two files is plugged in, your MyBookLive hard drive is hooked up to your computer via e-sata to sata or sata to sata cable and you disconnect any other hard drives to ensure you don’t accidentally delete data on those drives.

______________________________________________________________________________________________

UPDATED 10-9-2012

If you want to debrick the hard drive and KEEP YOUR EXISTING DATA on the data partition intact follow these steps

Now we want to boot off the SystemRescue CD you burned above. When the system menu comes up select to "directly start the graphical environment"

Once you get to the desktop, open gparted and check to see where your MyBookLive shows under. In my case it shows up under /dev/sda

The drive should contain several EXT3 partitions and a rather large EXT4 partition

If your drive shows up under sdb, sdc, sdd etc.  just substitute the proper name for the drive when getting to

./debrick.sh rootfs.img /dev/sda

Now in gparted check to see where your thumbdrive shows up. In my case it shows up under /dev/sdb1

If it shows up under sdc1, sdd1,sde1, etc.  just substitute the proper name for the drive when getting to

mount -t vfat /dev/sdb1 /mnt/usb

Terminal should already be opened on the screen, if not open terminal and type the following commands.

Yellow text indicates that you should use the changes from above for that particular part of the command if your devices show up under gparted differently than they did for me…

mkdir /mnt/usb                                            (Creates a mount directory)

mount -t vfat /dev/sdb1 /mnt/usb              (Mounts the thumbdrive with your 2 files)

cd /mnt/usb                                                  (Enters USB directory)

mdadm -S /dev/md0                                   (Stops SystemRescueCD Soft-RAID – REQUIRED for script to run)

./debrick.sh rootfs.img /dev/sda               (This command starts the debrick script WITHOUT DESTROYING your data)

Once the script has finished you can put the drive back in the enclosure and you should have a brand new fully restored MBL with all your data intact.

You now need to recreate your shares in the MyBookLive dashboard and you will have access to all your data again.

If your shares are still listed in the MyBookLive Dashboard but are not accessible, rename the shares and then rename them back to get them linked properly again.

EX. Movies > Movies1 > Movies

If you can’t remember your share names, follow these steps:

1) Enable SSH access on your MyBookLive by going to http://mybooklive/UI/ssh

2) Download WinSCP from http://winscp.net/download/winscp510setup.exe

3) Start WinSCP and when prompted for login info use Host: mybooklive  Username: root  Password: welc0me

4) Browse to the root of the drive then go to DataVolume/shares

5) Take note of all your folder names and recreate the EXACT folder names under Shares in the MyBookLive dashboard

_______________________________________________________________________________________________

If you want to debrick the hard drive and REBUILD ALL PARTITIONS AND DESTROY ALL DATA on your drive then run these commands instead. These commands will also allow you to install a brand new 1, 2, or 3tb drive into the Mybook Live enclosure.

Once again yellow text indicates that you should use the changes from above for that particular part of the command if your devices show up under gparted differently than they did for me…

mkdir /mnt/usb                                              (Creates mount directory)

mount -t vfat /dev/sdb1 /mnt/usb                (Mounts the thumbdrive with your 2 files)

cd /mnt/usb                                                    (Enters USB directory)

mdadm -S /dev/md0                                     (Stops SystemRescueCD soft-raid – REQUIRED for script to run)

./debrick.sh rootfs.img /dev/sda destroy  (Script will rewrite the partition table of disk and DESTROY all data and debrick the drive)

Here is what the output will look like with the destroy flag set. The MyBookLive works like a brand new drive as I have it up and running now

root@sysresccd /root % mkdir /mnt/usb
root@sysresccd /root % mount -t vfat /dev/sdc1 /mnt/usb
root@sysresccd /root % cd /mnt/usb
root@sysresccd /mnt/usb % mdadm -S /dev/md0
mdadm: stopped /dev/md0
root@sysresccd /mnt/usb % ./debrick.sh rootfs.img /dev/sda destroy

********************** DISK           **********************

script will use the following disk: 

Model: ATA WDC WD10EARS-00M (scsi)
Disk /dev/sda: 1000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 1      15.7MB  2064MB  2048MB  ext3         primary  raid
 2      2064MB  4113MB  2049MB  ext3         primary  raid
 3      4113MB  4624MB  512MB                primary
 4      4624MB  1000GB  996GB   ext4         primary

is this REALLY the disk you want? [y] y

********************** IMAGE          **********************


********************** IMPLEMENTATION **********************

everything is now prepared!
device:       /dev/sda
image_img:    rootfs.img
destroy:      true

this is the point of no return, continue? [y] y

32+0 records in
32+0 records out
33554432 bytes (34 MB) copied, 0.800974 s, 41.9 MB/s
32+0 records in
32+0 records out
33554432 bytes (34 MB) copied, 0.395157 s, 84.9 MB/s
32+0 records in
32+0 records out
33554432 bytes (34 MB) copied, 0.346756 s, 96.8 MB/s
32+0 records in
32+0 records out
33554432 bytes (34 MB) copied, 0.555298 s, 60.4 MB/s
Testing with pattern 0x00: done                                                 
Reading and comparing: done                                                 
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt                                                      
(parted) mkpart primary 528M  2576M                                       
(parted) mkpart primary 2576M 4624M                                       
(parted) mkpart primary 16M 528M                                          
(parted) mkpart primary 4624M -1M                                         
(parted) set 1 raid on                                                    
(parted) set 2 raid on                                                    
(parted) quit                                                             
Information: You may need to update /etc/fstab.

Warning: blocksize 65536 not usable on most systems.                      
mke2fs 1.42.3 (14-May-2012)
mkfs.ext4: 65536-byte blocks too big for system (max 4096)
Proceed anyway? (y,n) y
Warning: 65536-byte blocks too big for system (max 4096), forced to continue
Filesystem label=
OS type: Linux
Block size=65536 (log=6)
Fragment size=65536 (log=6)
Stride=0 blocks, Stripe width=0 blocks
15144960 inodes, 15191344 blocks
0 blocks (0.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=133677056
232 block groups
65528 blocks per group, 65528 fragments per group
65280 inodes per group
Superblock backups stored on blocks: 
	65528, 196584, 327640, 458696, 589752, 1638200, 1769256, 3210872, 
	5307768, 8191000

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   


destroying was done, would you like to continue with installation? [y] y

mdadm: size set to 1999808K
mdadm: array /dev/md0 started.
mke2fs 1.42.3 (14-May-2012)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
125184 inodes, 499952 blocks
24997 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=515899392
16 block groups
32768 blocks per group, 32768 fragments per group
7824 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

Checking for bad blocks (read-only test): done                                                 
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done 

mdadm: added /dev/sda2

synchronize raid... done

copying image to disk... 
3999616+0 records in
3999616+0 records out
2047803392 bytes (2.0 GB) copied, 170.032 s, 12.0 MB/s
mdadm: stopped /dev/md0

all done! device should be debricked!

root@sysresccd /mnt/usb % 

All credit for the script goes to Dan Kvelstad… You can see his thread at http://mybookworld.wikidot.com/forum/t-368098/debricking-script-that-can-keep-datahttp://

WDTV gen1 fw 1.03 – WDTV Live fw 1.06.15_V – WDTV Live Streaming fw 1.10.13 – Boxee Box fw 1.5.1.23734Mybook Live 3TB fw 02.11.09-053 – Mybook Live 1TB fw 02.32.05-044 – Seagate BlackArmor NAS 110 1TB fw 1000.1211Asus RT-N66U fw 3.0.0.3.151 -> Trendnet TEG-S80G 8 port Gigabit Switch -> Actiontec PWR511K01

页 2

08-21-2012 03:13 PM

I’m guessing it was a firmware update that bricked the dashboard? It would really help if we could tell what color the LED light is. Just stick the little plastic LED piece back in the rubber flap and see if you can tell what color we are dealing with. Please give the hard drive several minutes to fully boot up the first time. Also is it possible that the IP address changed on you? Can you ping mybooklive from a dos prompt? How are you trying to access the dashboard? I would try accessing it directly by its IP address. If you know your way around a router, see if your router is giving it an IP address under connected devices.

WDTV gen1 fw 1.03 – WDTV Live fw 1.06.15_V – WDTV Live Streaming fw 1.10.13 – Boxee Box fw 1.5.1.23734Mybook Live 3TB fw 02.11.09-053 – Mybook Live 1TB fw 02.32.05-044 – Seagate BlackArmor NAS 110 1TB fw 1000.1211Asus RT-N66U fw 3.0.0.3.151 -> Trendnet TEG-S80G 8 port Gigabit Switch -> Actiontec PWR511K01

Re: [GUIDE] How to unbrick a totally dead MBL

08-22-2012 09:51 AM

So the light is green but still nothing on the network.  My router is not even seeing the MBL.  I’m thinking even though the script ran and gave me the message that it has been debricked, there must have been errors during the process.  Someone on another forum had something similar:

"When I tried again I noticed that although the script appeared to complete (and I got the device should be debricked message), it was actually failing to complete the swap command at the end (the output displayed permission denied)."

I think this has something to do with my issue on mounting the USB.  I tried again last night and again I couldn’t mount the **bleep** this.  Did you have to change permissions?  I know for sure I’m using the right path to it.  

Re: [GUIDE] How to unbrick a totally dead MBL

[ Edited ]

08-22-2012 11:22 AM – edited 08-22-2012 12:36 PM

Nope I didn’t have to change any permissions. Only thing I can recommend is running the script again and looking for errors in the terminal window when done. I would think that if you were getting to a green light that everything should have went ok. Did you use the Ubuntu Live CD? Maybe try again using the System Rescue CD linked in my guide. It could be that the Ubuntu Live CD is missing some of the programs required for the script to complete properly such as mdadm.

Update: Looking around a bit it does appear that the Ubuntu Live CD DOES NOT include mdadm. So again I would suggest downloading the System Rescue CD linked in the guide. Please report back either way to let us know how you make out.

Good Luck

WDTV gen1 fw 1.03 – WDTV Live fw 1.06.15_V – WDTV Live Streaming fw 1.10.13 – Boxee Box fw 1.5.1.23734Mybook Live 3TB fw 02.11.09-053 – Mybook Live 1TB fw 02.32.05-044 – Seagate BlackArmor NAS 110 1TB fw 1000.1211Asus RT-N66U fw 3.0.0.3.151 -> Trendnet TEG-S80G 8 port Gigabit Switch -> Actiontec PWR511K01

Re: [GUIDE] How to unbrick a totally dead MBL

08-22-2012 03:12 PM

Unfortunately I have been using the SystemRecueCD.  I’ll try again tonight but last night I couldn’t even run the script because of the mounting problem…

Re: [GUIDE] How to unbrick a totally dead MBL

08-22-2012 03:37 PM

Is there any way you can hook the drive up to a sata connector in your computer instead of using the usb to sata connector? I wonder if that is what’s causing the mounting problems.

WDTV gen1 fw 1.03 – WDTV Live fw 1.06.15_V – WDTV Live Streaming fw 1.10.13 – Boxee Box fw 1.5.1.23734Mybook Live 3TB fw 02.11.09-053 – Mybook Live 1TB fw 02.32.05-044 – Seagate BlackArmor NAS 110 1TB fw 1000.1211Asus RT-N66U fw 3.0.0.3.151 -> Trendnet TEG-S80G 8 port Gigabit Switch -> Actiontec PWR511K01

Re: [GUIDE] How to unbrick a totally dead MBL

[ Edited ]

09-03-2012 08:29 AM – edited 09-03-2012 08:36 AM

Guide updated to show how to properly find and mount the usb thumbdrive. Would love to hear from anybody who has successfully debricked a MBL using this guide.

WDTV gen1 fw 1.03 – WDTV Live fw 1.06.15_V – WDTV Live Streaming fw 1.10.13 – Boxee Box fw 1.5.1.23734Mybook Live 3TB fw 02.11.09-053 – Mybook Live 1TB fw 02.32.05-044 – Seagate BlackArmor NAS 110 1TB fw 1000.1211Asus RT-N66U fw 3.0.0.3.151 -> Trendnet TEG-S80G 8 port Gigabit Switch -> Actiontec PWR511K01

Re: [GUIDE] How to unbrick a totally dead MBL

[ Edited ]

09-03-2012 01:27 PM – edited 09-03-2012 04:52 PM

Guide updated again… Today I was bored so I debricked a 1TB MBL with the guide and posted a proper script output file to the bottom of the guide. Compare your output to make sure your not running into any errors. The MBL booted up like a brand new drive after about 5 minutes and was accessible on my network with no problems at all. I also noticed SSH was enabled by default now.

WDTV gen1 fw 1.03 – WDTV Live fw 1.06.15_V – WDTV Live Streaming fw 1.10.13 – Boxee Box fw 1.5.1.23734Mybook Live 3TB fw 02.11.09-053 – Mybook Live 1TB fw 02.32.05-044 – Seagate BlackArmor NAS 110 1TB fw 1000.1211Asus RT-N66U fw 3.0.0.3.151 -> Trendnet TEG-S80G 8 port Gigabit Switch -> Actiontec PWR511K01

Re: [GUIDE] How to unbrick a totally dead MBL

09-03-2012 04:22 PM

I also think I have had the problem of a firmware update bricking my drive, white light and no dashboard.

I have removed the drive and connected it to my computer and can see the drive using Linux Reader.

I have not used linux very much and I would like to copy the data that was on it but can’t work out which directory it is stored in.

Which partition and directory has the data?

Also what is the best way to wipe the data as WD have sent me a replacement drive and allowed me to open the case to acces the hard disk inside and I want to copy the data onto the new drive and wipe the data off the old drive.

Re: [GUIDE] How to unbrick a totally dead MBL

[ Edited ]

09-03-2012 04:35 PM – edited 09-04-2012 01:27 PM

It’s been a bit since I used Disk Internals Linux Reader, can you tell me the directories you are seeing to maybe refresh my memory?

Try

CacheVolume\shares\

DataVolume\shares\

WDTV gen1 fw 1.03 – WDTV Live fw 1.06.15_V – WDTV Live Streaming fw 1.10.13 – Boxee Box fw 1.5.1.23734Mybook Live 3TB fw 02.11.09-053 – Mybook Live 1TB fw 02.32.05-044 – Seagate BlackArmor NAS 110 1TB fw 1000.1211Asus RT-N66U fw 3.0.0.3.151 -> Trendnet TEG-S80G 8 port Gigabit Switch -> Actiontec PWR511K01

页 3

[ Edited ]

09-26-2012 02:19 PM – edited 09-26-2012 02:49 PM

I try to debrick my MBL but I get this screen, I dont know if I do it right

********************** DISK **********************
script will use the following disk:
Model: ATA WDC WD20EARX-00P (scsi)
Disk /dev/sda: 2000GB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
3 15.7MB 528MB 513MB primary
1 528MB 2576MB 2048MB ext3 primary raid
2 2576MB 4624MB 2048MB ext3 primary raid
4 4624MB 2000GB 1996GB ext4 primary
is this REALLY the disk you want? [y] y
********************** IMAGE **********************

********************** IMPLEMENTATION **********************
everything is now prepared!
device: /dev/sda
image_img: rootfs.img
destroy: false
this is the point of no return, continue? [y] y

mdadm: /dev/sda1 appears to contain an ext2fs file system
size=1999808K mtime=Wed Sep 26 15:09:29 2012
mdadm: size set to 1999808K
mdadm: creation continuing despite oddities due to --run
mdadm: array /dev/md0 started.
mke2fs 1.42.5 (29-Jul-2012)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
125184 inodes, 499952 blocks
24997 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=515899392
16 block groups
32768 blocks per group, 32768 fragments per group
7824 inodes per group
Superblock backups stored on blocks: 
32768, 98304, 163840, 229376, 294912
Checking for bad blocks (read-only test): 0.00% done, 0:00 elapsed. (0/0/0 errdone 
Allocating group tables: done 
Writing inode tables: done 
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
mdadm: added /dev/sda2
synchronize raid... done
copying image to disk... 
dd: writing to `/dev/md0': Input/output error
1041+0 records in
1040+0 records out
532480 bytes (532 kB) copied, 16.0753 s, 33.1 kB/s
cp: cannot stat `/mnt/md0/usr/local/share/bootmd0.scr': No such file or directory ./debrick.sh: line 359: /mnt/md0/etc/nas/service_startup/ssh: No such file or directory
mdadm: stopped /dev/md0
all done! device should be debricked!

 the section in italic and underline is that I dont understand

help me please

页 4

a week ago

Thank you for your steps and instructions.

It worked for me!

A couple things though-

I was able to copy over most of my vital documents.  Then went through the process.  No problems there.

I choose to preserve the shares and when I loaded it back onto the network and went to the Dashboard, a lot of the settings were already there.  My user names, the accounts they were tied to through WD2go, and the shares were all listed as already created.  However, they were not linked properly by the OS and were not actually being shared.

I deleted the user accounts.  I then attempted to restore one of the shares by deleting it on the dashboard and re-creating it with the same name.  That deleted all of the files in the share, but then shared the folder.  (I had already backed up that data.)

I then renamed one of the shares "Photos" to "Photos2" and made it public rather than private.  That made it appear on the network drive.  I then was able to name it back to "Photos" and my drive mappings occured like they had previously.

I am unable to make it back to a private share though, it keeps encountering an error.  But I haven’t attempted to troubleshoot it because I am just waiting for the replacement warrenty drive to copy it all over.

The drive also did a self check and is registering itself as "Bad" with a SMARTware failure.  It also will not update the firmware from the version I put on with the debricking script.

So it worked to get my data back, but I am waiting on the new drive to just copy my files over onto and send this one back.

页 5

13 hours ago

nfodiz wrote:

First try Version 2 of the script

http://www.mediafire.com/?jd78qi8oiq8w35s

Please report back

root@sysresccd /root % mkdir /mnt/usb
root@sysresccd /root % mount -t vfat /dev/sdb1 /mnt/usb
root@sysresccd /root % cd /mnt/usb
root@sysresccd /mnt/usb % mdadm -S /dev/md0                       
mdadm: stopped /dev/md0
root@sysresccd /mnt/usb % ./debrick.sh rootfs.img /dev/sda destroy


/dev/md0 already exists! you need to stop and remove it.

root@sysresccd /mnt/usb % mdadm -S /dev/md0                       
mdadm: stopped /dev/md0
root@sysresccd /mnt/usb % ./debrick.sh rootfs.img /dev/sda destroy

********************** DISK           **********************

script will use the following disk: 

Model: ATA WDC WD20EARS-00M (scsi)
Disk /dev/sda: 2000GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 

Number  Start   End     Size    File system  Name     Flags
 3      15.7MB  528MB   513MB                primary
 1      528MB   2576MB  2048MB  ext3         primary  raid
 2      2576MB  4624MB  2048MB  ext3         primary  raid
 4      4624MB  2000GB  1996GB  ext4         primary

is this REALLY the disk you want? [y] y

********************** IMAGE          **********************


********************** IMPLEMENTATION **********************

everything is now prepared!
device:       /dev/sda
image_img:    rootfs.img
destroy:      true

this is the point of no return, continue? [y] y

32+0 records in
32+0 records out
33554432 bytes (34 MB) copied, 0.739789 s, 45.4 MB/s
32+0 records in
32+0 records out
33554432 bytes (34 MB) copied, 0.475673 s, 70.5 MB/s
32+0 records in
32+0 records out
33554432 bytes (34 MB) copied, 0.42701 s, 78.6 MB/s
32+0 records in
32+0 records out
33554432 bytes (34 MB) copied, 0.556052 s, 60.3 MB/s
Testing with pattern 0x00: done                                                 
Reading and comparing: done                                                 
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt                                                      
(parted) mkpart primary 528M  2576M                                       
(parted) mkpart primary 2576M 4624M                                       
(parted) mkpart primary 16M 528M                                          
(parted) mkpart primary 4624M -1M                                         
(parted) set 1 raid on                                                    
(parted) set 2 raid on                                                    
(parted) quit                                                             
Information: You may need to update /etc/fstab.

Warning: blocksize 65536 not usable on most systems.                      
mke2fs 1.42.6 (21-Sep-2012)
mkfs.ext4: 65536-byte blocks too big for system (max 4096)
Proceed anyway? (y,n) y
Warning: 65536-byte blocks too big for system (max 4096), forced to continue
Filesystem label=
OS type: Linux
Block size=65536 (log=6)
Fragment size=65536 (log=6)
Stride=0 blocks, Stripe width=0 blocks
30355200 inodes, 30453088 blocks
0 blocks (0.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=133677056
465 block groups
65528 blocks per group, 65528 fragments per group
65280 inodes per group
Superblock backups stored on blocks: 
	65528, 196584, 327640, 458696, 589752, 1638200, 1769256, 3210872, 
	5307768, 8191000, 15923304, 22476104

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   


destroying was done, would you like to continue with installation? [y] y

mdadm: size set to 1999808K
mdadm: array /dev/md0 started.
mke2fs 1.42.6 (21-Sep-2012)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
125184 inodes, 499952 blocks
24997 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=515899392
16 block groups
32768 blocks per group, 32768 fragments per group
7824 inodes per group
Superblock backups stored on blocks: 
	32768, 98304, 163840, 229376, 294912

Checking for bad blocks (read-only test):   0.00% done, 0:00 elapsed. (0/0/0 errdone                                                 
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done 

mdadm: added /dev/sda2

synchronize raid... done

copying image to disk... 
dd: writing to ‘/dev/md0’: Input/output error
659593+0 records in
659592+0 records out
337711104 bytes (338 MB) copied, 45.0055 s, 7.5 MB/s
cp: accessing ‘/mnt/md0/boot/boot.scr’: Input/output error
./debrick.sh: line 359: /mnt/md0/etc/nas/service_startup/ssh: Input/output error
mdadm: stopped /dev/md0

all done! device should be debricked!

root@sysresccd /mnt/usb % 

 Again failure. I tried that that you threw off, but again the same mistake

页 6

[ Edited ]

12 hours ago – last edited 11 hours ago

Did this command complete without error?

dd if=/mnt/mybook/mybook.img of=/dev/sda bs=1M

Terminal should say something close to this

4700+0 records in

4700+0 records out

4928307200 bytes (4.9 GB) copied, 185.112 s, 26.6 MB/s

If so you can put the board back on the drive and try booting up the drive

Please keep me updated

WDTV gen1 fw 1.03 – WDTV Live fw 1.06.15_V – WDTV Live Streaming fw 1.10.13 – Boxee Box fw 1.5.1.23734Mybook Live 3TB fw 02.11.09-053 – Mybook Live 1TB fw 02.32.05-044 – Seagate BlackArmor NAS 110 1TB fw 1000.1211Asus RT-N66U fw 3.0.0.3.151 -> Trendnet TEG-S80G 8 port Gigabit Switch -> Actiontec PWR511K01

Google Analytics Tracking for Adobe Flash – Google Analytics — Google Developers

Google Analytics Tracking for Adobe Flash

The Google Analytics Tracking for Adobe Flash component makes it easy for
you to implement Google Analytics in your Flash-driven content. This component,
developed by Adobe Systems, Inc., contains all of the functionality of the
Google Analytics Javascript code. The Flash Tracking component is a compiled
tracking object native to ActionScript 3, making Analytics implementation intuitive
in Flash, and Flex development environments.

The software for this component is developed
and supported by Adobe Systems, Inc.
Google makes no promises or representations about its performance, quality,
or content.

If you are a developer interested in contributing to the project, visit the project home page.

http://code.google.com/p/gaforflash/

Why Use Flash Tracking?

Without the Google Analytics Tracking for Adobe Flash component, tracking Adobe Flash content with Google Analytics involves a number of technical hurdles. First, you must develop a custom interface to ga.js so that your Flash application can execute the appropriate Analytics method, such as trackPageview() or trackEvent(). In addition, you must also anticipate whether your Flash content will have access to the browser Document Object Model (DOM), since tracking fails for those objects where access to the DOM is denied (typically when your content resides on 3rd party sites). This involves understanding how to use the ExternalInterface call in ActionScript 3 to access the browser DOM and to degrade when access is denied.

The Google Analytics for Adobe Flash component simplifies tracking your Flash content and handles DOM access gracefully. It is useful for a number of common tracking purposes in Flash, such as: :

  • an embedded Flash widget on an HTML page
  • a standalone Flex application or Flash-only site hosted on an HTML page
  • a distributed Flex/Flash game or program where the developer has no control over where the widget will be placed

Keep in mind that tracking applications in Flash has some structural variations from tracking website pages. Familiarity with Analytics Tracking is essential to understanding how this plug-in works. You can also view the Design Documentation for this project for detailed information on how the Analytics Tracking model has been ported over for this component.

Note: Currently, Flash tracking is available for any Flash content embedded in a web page. Tracking of data sent from Adobe Air, Shockwave, or via the Flash IDE (e.g. using Test Movie) is not supported at this time.

Supported Development Environments

You can develop Analytics Tracking for Flash in either Adobe Flash or Adobe Flex environments. Each environment requires a different component, which you can download from http://code.google.com/p/gaforflash/ . These components are based on ActionScript 3 and can be set up in one of two ways for each environment:

In Adobe Flash

  • Add and configure a simple component in the component inspector and drag it to the stage.
  • Import the Flash Tracking libraries directly into your library and start coding.

In Adobe Flex

  • Include an MXML component that you configure from am MXML file.
  • Import the Flash Tracking libraries into your script tags/AS3 files.

Back to Top

How Does the Component Work?

In order to use the Flash tracking component in your environment, you either use the visual tools inside Flash, or you set up the tracking object directly in your code. Regardless of whether you are setting up the component visually or via code, you provide the following elements:

  • the web property ID—This is also known as the UA number of your tracking code and looks like UA-xxxxx-yy, where the x’s and y’s are replaced with the numbers that correspond to your account and profile information for the object you are tracking. See Web Property for more information.
  • the tracking mode—Choose either bridge mode or AS3 mode. This mode determines how your tracking communicates with the Analytics servers and is described in detail below.
  • the debugging mode—No matter which environment or tracking mode you use, you can turn debugging on to validate and test your tracking.

Tracking Modes

Depending upon on how you distribute your Flash content, the Analytics for Flash component communicates to the Analytics servers either by bridging the communication between the Flash content on an existing Analytics tracking installation, or by communicating directly to the Analytics servers. These two modes are called bridge mode and AS3 mode, respectively. Both modes use the same Analytics tracking functionality, and it’s easy to switch your Flash application from one mode to the other. In addition to choosing a communication mode for Analytics tracking, you can also use a debug mode to troubleshoot or validate your tracking.

In either mode, allowscriptaccess must equal always in order for campaign tracking to work. This parameter turns on read access to the page’s URL and referrer information required by the Flash tracking code. Without allowscriptaccess, the Analytics tracking code degrades gracefully. It still provides most user activity data, but will not confirm to the Google Analytics campaign attribution model.

Bridge Mode

Use this mode if you control both the HTML page and the Flash content. This mode is best if you have already implemented Google Analytics (ga.js) tracking on your website and you want to add tracking to embedded Flash content. The bridge mode simplifies Flash-to-JavaScript communication by providing a unified ActionScript 3 interface to the ga.js code. It provides the connection from the ActionScript 3 calls to the Analytics JavaScript in order to make the tracking work.

The connection to the Google Analytics Tracking Code can be configured through the web property ID parameter in one of two ways:

  • Most common method. The Google Analytics Tracking Code
    object already exists on your page with its own name, such as pageTracker.
    In this case, you provide the full DOM reference to the tracking object.
    For example, if your object is called pageTracker, you would
    reference that object in your code as window.pageTracker. For
    example, the following code snippet illustrates how this would be configured
    using the Adobe Flex environment with ActionScript 3:

    tracker = new GATracker( this, "window.pageTracker", "Bridge", false );
  • Alternate method. If you have not created a page tracking object on your page, you can simply pass in your web property ID, and a JavaScript tracking code object will be created for you. With this method, reference to the base ga.js javascript source file is still required on your HTML page. The following code snippet illustrates how this would be configured using the Adobe Flex environment with ActionScript 3:
    tracker = new GATracker( this, "UA-12345-22", "Bridge", false );

In order for bridge mode to function correctly, ExternalInterface.available must
be set to true in your ActionScript 3 code. This also means that allowScriptAccess should
be set to always in the HTML page that embeds the Flash content.
The following example illustrates HTML code configured for bridge mode:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
     id="flex_component" width="800" height="600"
     codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
     <param name="movie" value="flex_component.swf" />
     <param name="quality" value="high" />
     <param name="bgcolor" value="#869ca7" />
     <param name="allowScriptAccess" value="always" />
     <embed src="flex_component.swf" quality="high" bgcolor="#869ca7"
         width="800" height="600" name="flex_component" align="middle"
         play="true"
         loop="false"
         quality="high"
         allowScriptAccess="always"
         type="application/x-shockwave-flash"
         pluginspage="http://www.adobe.com/go/getflashplayer">
      </embed>
</object>

Back to Top

AS3 Mode

Use this mode if you control the Adobe Flash ActionScript 3 code, but you do not control the hosting environment of your Adobe Flash application. For example, if you are developing Flash content for distribution across many sites, then you would use AS3 mode. AS3 mode is completely independent from the ga.js tracking code and contains all the Analytics tracking functionality. There is no need for a separate ga.js tracking installation with this mode. In addition, AS3 mode uses the Flash storage mechanism to track session information for the visitor.

For certain DOM parameters such as language, the AS3 component tries to retrieve the values from the browser. If the values are not present, the component either uses the Flash equivalent value or defaults to no.

Troubleshooting and Validation

The Google Analytics Tracking for Adobe Flash component provides a debug mode to simplify validation and troubleshooting. When enabled, all tracking data is intercepted and directed to a screen in a text box instead of the Analytics servers. In this mode, you can see real-time the data that otherwise would be collected by the server. This feature also helps keep test data outside of your production data. You can enable the troubleshooting feature can by setting the visualDebug option to true in the component inspector.

Examples

For detailed examples on implementing tracking in the different development environments, see the following:

Flash

Flex

Versioning

You can find the latest version of the tracking components as a ZIP file at http://code.google.com/p/gaforflash/downloads/list . Each download contains all tracking components along with related documentation. The file name for the download file will display the version number of the code it contains.

You can also use the following statements in your code to print the component’s current version number into the output console.

import com.google.analytics.API;
trace(API.version); 

Back to Top