سؤال

I'm trying to test Google Analytics measurement protocol, specifically transactions. I've successfully setup a simple ajax call that is posting to google analytics, but in order to pass a custom metric, you have to use an asterisk (*) and define which slot (1-20) that the metric will fall into. As soon as I try to post, I get an error: "Uncaught SyntaxError: Unexpected token *"

Here is what my call looks like:

$.ajax({
    url: 'http://google-analytics.com/collect',
    type: 'POST',
    data: {
       v: '1',
       tid: 'UA-3679639-17',
       cid: '1974905664.1397752793',
       t: 'transaction',
       ti: '456',
       tr: '100.00',
       tt: '5.50',
       ts: '9.99',
       cm*2: '50.00',
       cm*3: '50.00'
    },
    success: function(){
        alert('success');
    },
    error: function(){
        alert('fail');
    }
});

Is there a way to encode the asterisk?

هل كانت مفيدة؟

المحلول

That's a javascript error telling you that * is not a valid character in an objects key unless it's quoted as a string

$.ajax({
    url: 'http://google-analytics.com/collect',
    type: 'POST',
    data: {
       v: '1',
       tid: 'UA-3679639-17',
       cid: '1974905664.1397752793',
       t: 'transaction',
       ti: '456',
       tr: '100.00',
       tt: '5.50',
       ts: '9.99',
       'cm*2': '50.00', // needs quotes
       'cm*3': '50.00'
    },
    success: function(){
        alert('success');
    },
    error: function(){
        alert('fail');
    }
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top