1

I am working on a project in Google spreadsheet and are using a script which will trigger activity on the sheet like Edit/Delete/NewLine etc.. and these activities are sent over to Google Analytics with Measurements Protocol.

Now i have a If...Else if...Else which determines that this is an edit, newLine or deleted existing value.

This is my Code:
// UPDATED code after answer, but still not working.

    var GoogleAnalyticsEDIT;
var GoogleAnalyticsSLETT;
var GoogleAnalyticsREDIGER;


// MP
// EDIT SEND
GoogleAnalyticsEDIT = function GoogleAnalyticsEDIT(tid){
  var data = {   'v': '1',
                 't' : 'event',
                 'tid': tid,          // TrackingID Google Analytics
                 'cid': Session.getTemporaryActiveUserKey(),
                 'ec': 'SpreadSheet', // Event Kategori.
                 'ea': 'nyVerdi-'+noLetter,    // Event Hendelse(action) 
                 'el':  SpreadsheetApp.getActiveSpreadsheet().getName(),
                 'ev': '300' };       // test og fjern                                                               <----- TEST OG FJERN
  var payload = Object.keys(data).map(function(key) {
                                        return encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
                                    }).join('&');
  var options = {'method' : 'POST',
                 'payload' : payload };
  UrlFetchApp.fetch('http://www.google-analytics.com/collect', options); 
}



//MP
// SLETT SEND
GoogleAnalyticsSLETT = function GoogleAnalyticsSLETT(tid){
  var data = {   'v': '1',
                 't' : 'event',
                 'tid': tid,          // TrackingID Google Analytics
                 'cid': Session.getTemporaryActiveUserKey(),
                 'ec': 'SpreadSheet', // Event Kategori.
                 'ea': 'slettVerdi-'+noLetter,    // Event Hendelse(action) 
                 'el':  SpreadsheetApp.getActiveSpreadsheet().getName(),
                 'ev': '300' };       // test og fjern                                                               <----- TEST OG FJERN
  var payload = Object.keys(data).map(function(key) {
                                        return encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
                                    }).join('&');
  var options = {'method' : 'POST',
                 'payload' : payload };
  UrlFetchApp.fetch('http://www.google-analytics.com/collect', options); 
}   




// MP  
// REDIGER SEND
GoogleAnalyticsREDIGER = function GoogleAnalyticsREDIGER(tid){
  var data = {   'v': '1',
                 't' : 'event',
                 'tid': tid,          // TrackingID Google Analytics
                 'cid': Session.getTemporaryActiveUserKey(),
                 'ec': 'SpreadSheet', // Event Kategori.
                 'ea': 'redigerVerdi-'+noLetter,    // Event Hendelse(action)
                 'el':  SpreadsheetApp.getActiveSpreadsheet().getName(),
                  };       // test og fjern                                                               <----- TEST OG FJERN
  var payload = Object.keys(data).map(function(key) {
                                        return encodeURIComponent(key) + '=' + encodeURIComponent(data[key]);
                                    }).join('&');
  var options = {'method' : 'POST',
                 'payload' : payload }; 
  UrlFetchApp.fetch('http://www.google-analytics.com/collect', options); 
}   




//FUNKSJON ---- DEBUG
var newValue = (typeof e.value == "object" ? e.range.getValue() : e.value); 
var oldValue = (typeof e.oldValue == "object" ? e.range.getValue() : e.oldValue); 


  if (!e.oldValue ) {
    Browser.msgBox("GA: newValue (NY LINJE)");
   GoogleAnalyticsEDIT("UA-101502909-2"); 
    }

  else if (e.value.hasOwnProperty("oldValue")) {
   GoogleAnalyticsSLETT("UA-101502909-2");
     }

  else { 
    Browser.msgBox("GA: ChangeValue (REDIGERT VERDI)"); 
   GoogleAnalyticsREDIGER("UA-101502909-2")

    }


} //slutt: MyOnEditNeW();

Problem: The first IF(..) statement works, i get the msgbox alert and data is sent to Google Analytics, BUT NOT the second blow If Else(...) block. The msgbox alert works but the GoogleAnalyticsSLETT(); function is not triggered. This is a installable-trigger

Anyone knows that maybe it's not allowed to call function in If Else statement, or is my code wrong?

1
  • You should not use Browser.msgBox() for debugging. Use Logger.log('variableName: ' + variableName) Run the code and then VIEW the LOGS. Also, what does the Execution Transcript state? What line of code fails. We don't want to know the line number. We don't know the line numbers of your code. Commented Aug 1, 2017 at 18:58

1 Answer 1

2

I'd probably change this: else if (e.value.hasOwnProperty("oldValue")) to this else if (e.oldvalue)

Sign up to request clarification or add additional context in comments.

2 Comments

not working as expected when i try this method. When new value is added the key has oldValue in string. So if there is nothing in value then the condition is else
You actually saved me. this was the problem. changed it to e.oldValue && e.value on else if statement now it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.