Google Analytics Plugin Not Logging Events

So, the Google Analytics plugin is set up correct I believe as

googleAnalytics.logScreenName( composer.getSceneName( "current" ) )

is working perfectly. However… I can’t for the life of me get it to log events!

    googleAnalytics.logEvent( "userAction", "button press", "menuItem", 2 )

I’ve tried the above code, and several other variations. I’ve tried entering in each one of these into goals… but when I look at my realtime analytics, my goals, and my events nothing shows up in GA.

Is there something that I am doing wrong? I don’t understand how I can log the screen name but I can’t log events…

I also have the Flashlight and Facebook plugins installed. Could one these be causing issues?

Any help would be greatly appreciated.

Thanks!

So after a few hours of comparing my project to the example project…

https://github.com/coronalabs/plugins-sample-googleAnalytics

I found that the problem was that you couldn’t have the require in an if statement. I commented out the if, which causes my simulator to stop working, and boom… it works.

This was my old code that didn’t work…

if ( system.getInfo( "environment" ) ~= "simulator" ) then local googleAnalytics = require( "plugin.googleAnalytics" ) end

You did not state in your first post which environment you were executing this code. If you were executing it in the simulator that would indicate why the second post is leading you up the garden path. You certainly can have the require statement in if blocks, no doubt about it. Your problem, I suspect, is that you are running in the simulator and this causes the googleAnalytics plugin not to be loaded. If you are running in the real device this symptom would point to something else. If you really do believe that require() can’t be used in an if block it is very easy to check, however that is not the case.

I was running it on my device. Commenting out the if block was the only thing that made this work. Maybe I needed to declare 

local googleAnalytics

and then do

if ( system.getInfo( “environment” ) ~= “simulator” ) then

local googleAnalytics = require( “plugin.googleAnalytics” )
end

before the if block?

But seriously I was running it on an adhoc build on my iPhone 6.

Argh. Can’t believe I missed that. (It’s damn early where I am.)

Ok, so the problem here is not the require but the use of local. Declaring a variable local within an if statement means that you can’t use it outside of that if statement. Effectively, the vairable googleAnalytics ceases to exist once the if’s end is reached.

Don’t add local to the top of the file, simply declare the local variable at the top and remove local from within the if statement, like this:

-- declare variable which is local to this file local googleAnalytics -- if we are running on a simulator then don't try to load google analytics if ( system.getInfo( "environment" ) ~= "simulator" ) then -- we are not running on sim, so load GA googleAnalytics = require( "plugin.googleAnalytics" ) end -- somewhere else in this file we will call GA and log some analytics... -- ...but only if the googleAnalytics variable is not nil if (googleAnalytics) then googleAnalytics.logEvent( "userAction", "button press", "menuItem", 2 ) end

So after a few hours of comparing my project to the example project…

https://github.com/coronalabs/plugins-sample-googleAnalytics

I found that the problem was that you couldn’t have the require in an if statement. I commented out the if, which causes my simulator to stop working, and boom… it works.

This was my old code that didn’t work…

if ( system.getInfo( "environment" ) ~= "simulator" ) then local googleAnalytics = require( "plugin.googleAnalytics" ) end

You did not state in your first post which environment you were executing this code. If you were executing it in the simulator that would indicate why the second post is leading you up the garden path. You certainly can have the require statement in if blocks, no doubt about it. Your problem, I suspect, is that you are running in the simulator and this causes the googleAnalytics plugin not to be loaded. If you are running in the real device this symptom would point to something else. If you really do believe that require() can’t be used in an if block it is very easy to check, however that is not the case.

I was running it on my device. Commenting out the if block was the only thing that made this work. Maybe I needed to declare 

local googleAnalytics

and then do

if ( system.getInfo( “environment” ) ~= “simulator” ) then

local googleAnalytics = require( “plugin.googleAnalytics” )
end

before the if block?

But seriously I was running it on an adhoc build on my iPhone 6.

Argh. Can’t believe I missed that. (It’s damn early where I am.)

Ok, so the problem here is not the require but the use of local. Declaring a variable local within an if statement means that you can’t use it outside of that if statement. Effectively, the vairable googleAnalytics ceases to exist once the if’s end is reached.

Don’t add local to the top of the file, simply declare the local variable at the top and remove local from within the if statement, like this:

-- declare variable which is local to this file local googleAnalytics -- if we are running on a simulator then don't try to load google analytics if ( system.getInfo( "environment" ) ~= "simulator" ) then -- we are not running on sim, so load GA googleAnalytics = require( "plugin.googleAnalytics" ) end -- somewhere else in this file we will call GA and log some analytics... -- ...but only if the googleAnalytics variable is not nil if (googleAnalytics) then googleAnalytics.logEvent( "userAction", "button press", "menuItem", 2 ) end