English 中文(简体)
如何确保谷歌分析习惯 隔离墙太长
原标题:How to make sure Google Analytics customVars are not too long

http://code.google.com/apis/analytics/docs/履带/gaTrackingCustomVariables.html#varTypes”rel=“nofollow” 页: 1

The total combined length of any custom variable name and value may not exceed 64 bytes. Keep in mind that this is not equivalent to 64 characters. Because names and values are URI encoded when stored, some characters use more than one byte. For example, = is stored as %3D rather than = and uses 3 bytes rather than 1. To get a list of URI encoded values, search the web for URL encoding reference.

I have two questions about that:

  1. Should an encoded equal sign (=) be included in those 64 bytes?
  2. I ve tried to make a function that makes sure the custom name and value is not too long. Can it be improved? (Of course it can.)

    function truncateCustomVarAndSet(index, name, value, scope) {
        var keyValuePair, 
            encodedPair,
            lengthOK = false;
        while (!lengthOK && value.length > 0) {
            keyValuePair = name +  =  + value;
            encodedPair = encodeURIComponent(keyValuePair);
            lengthOK = encodedPair.length <= 64;
            if (!lengthOK) {
                value = value.substring(0, value.length - 1);
            }
        } 
        _gaq.push([ _setCustomVar , index, name, value, scope]);        
    } 
    

<><>Edit>: 现在使用的是encodeURIComponent,而不是encodeURI

<Edit2:@yahelc change gaq to _qac, 因此,我从论点清单中删除了“gaq”一词,因为现在更不需要。

最佳回答
问题回答
  1. 从描述中看,“=”似乎只是作为设定的一部分计算,只有在关键或价值相同的情况下。

  2. 我把你的职能缩减了一步。

function truncateCustomVarAndSet(index, name, value, scope) {
    var n = encodeURIComponent(name)
        ,v = encodeURIComponent(value)
    for(;(n + v).length > 64; v=encodeURIComponent(value=value.substr(1-value.length)));
    _gaq.push([ _setCustomVar , index, name, value, scope]);        
} 




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签