Track Admob Event in Google Analytics - android

I want to track clicks on an AdMob banners with Google Analytics, but a problem occurs and I don't understand why.
Currently, my AdMob banner is implemented like this:
Layout:
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adUnitId="YOUR_AD_UNIT_ID"
ads:adSize="BANNER"/>
Java : AdView adView = (AdView)this.findViewById(R.id.adView);
However, the Google demonstration project which shows how to add an AdListener (project available here) does not specify anything in the layout and use the following code to add the banner:
LinearLayout layout = (LinearLayout) findViewById(R.id.leLinearLayoutDeMonChoix);
layout.addView(adView);
But if the implementation described at the beginning is used, the AdListener does not detect any event anymore. Why?
You can find this defective implementation in the following demonstration project: https://drive.google.com/file/d/0B8rE1pbtzNJ1UXg5QllubEFidGc/edit?usp=sharing
I thank you in advance for your time and help.

In the provided implementation you are doing the following:
// Create an ad.
adView = new AdView(this);
// Set the AdListener.
adView.setAdListener(new AdListener() {
/** stuff **/
}
AdView adView = (AdView)this.findViewById(R.id.adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
There is two instances of AdView, and this is your problem.
first one created from inflating your main.xml layout by setContentView(),
second one when you wrote adView = new AdView(this);
You set your listener on the second one, but only the first one is displayed.
It cannot work. :)
Choose one method (create it from layout) or the other (create it from code), but don't mix them up.
If you want to create your ad from layout, do the following:
// Retreive the adView.
AdView adView = (AdView)this.findViewById(R.id.adView);
// Set the AdListener.
adView.setAdListener(new AdListener() {
/** stuff **/
}
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);

Related

Is there any special way of implementing admob ads?

I am trying to add admob ads to my app by whenever i try the ads do not show but when i use the test ads it shows. I have also cheked my admob account it has been approved for displaying ads
I have tried using the admob test ads and it shows perfectly
Here is an example of the banner ad
mAdView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
I expect it to show my ads but it shows only the test ads whenever I replace my ad unit with the test ad unit.
Try to confirm that you have used valid banner ad id, If you still not getting real ad wait sometimes and test in another device. Hope it will work
-----------:Add custom layout:--------------
<!--Admob Banner Ad-->
<RelativeLayout
android:id="#+id/rlAdViewBannerAdmob"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
public AdView ad;
AdRequest adRequest;
//init Ad
ad =new AdView(context);
ad.setAdSize(AdSize.SMART_BANNER);
ad.setAdUnitId(adUnitId);
//Load ad
adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice("14C739553C9657EB85559903949D428B") //add test Ids
.build();
//load ad
ad.loadAd(adRequest);
rlAdViewBannerAdmob.addView(ad); // add loaded ad in customview
//do not forget to add *YOUR_ADMOB_APP_ID* in manifest
<manifest>
<application>
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="YOUR_ADMOB_APP_ID"/>
</application>
</manifest>

Need to put AdMob ad id in java code? (Android Studio)

For example, on one of my app screens where you have a Banner ad, you have the following code in the xml layout:
<com.google.android.gms.ads.AdView
android:id="#+id/adViewPrincipal"
android:layout_width="0dp"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/adId" />
And also in java code has the following code:
MobileAds.initialize(this, String.valueOf(R.string.adId));
adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
It seems that everything is working with this test id, but when I take the ID parameter in this java code, leaving:
MobileAds.initialize(this);
or
MobileAds.initialize(this, "");
The ad keeps running without any errors in the app, so do I just need to set the ad id in XML? Is that in all the tutorials I've seen people putting the ID also in java code. If I leave the parameter with nothing, nothing goes wrong, it's like this parameter is useless, and when I get the ID from the XML code, that crashes app. Can I put the ad id only in XML code then?
You are passing the second argument of initialize wrong. It is supposed to be app-id of admob on ad-unit id. The ad id is used normally only in xml views, where app_id is used in code to initialize ads for the first time. Read More here
MobileAds.initialize(this, getResources().getString(R.string.app_id));
Can you try everything programmatically:-
LinearLayout adContainer = <container>;
AdView adView = new AdView(activity)
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(<your-publisher-id>); // you can use id from constants
// Initiate a generic request to load it with an ad
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
adView.loadAd(adRequest);
// Place the ad view.
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
adContainer.addView(adView, params);

Admob not working at all:The Google Play services resources were not found.Check your project configuration

I have tried everything, applied every solution I found. Checked googleplayservices many times. Still admob is not working.here I see no need to worry.
Does that mean if I submit my app, admob will work fine after submitting? If not here is my code for admob,how do I fix this?
private AdView adView;
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("My App Ad Unit ID");
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.adViewLayout);
layout.addView(adView);
// Create an ad request. Check logcat output for the hashed device ID to
// get test ads on a physical device.
AdRequest adRequest = new AdRequest.Builder()
// .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
// .addTestDevice("52EEC3DAD2FA75F22B44407D19072632")
.build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
XML:
<LinearLayout
android:id="#+id/adViewLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
</LinearLayout>
This is what the google guys say over it
I keep getting the error 'The Google Play services resources were not found. Check your project configuration to ensure that the resources are included.'
You can safely ignore this message. Your app will still fetch and serve banner ads.
Also this has bee asked several times so just ignore it

Android AdMob cannot set into testing mod

the line adView.setTesting(true); gives e a error saying the method does not exits,
code blocksetTesting
adView = new AdView(this, AdSize.BANNER, "a14e10cb6b18825");
ad = new AdView(this, AdSize.BANNER, "a14e10cb6b18825");
// Lookup your LinearLayout assuming it’s been given
// the attribute android:id="#+id/mainLayout"
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.setTesting(true);
adView.loadAd(new AdRequest());
I'm unfamiliar with the setTesting() api call. In my AdMob code, I use
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(MY_TEST_DEVICE_ID);
adView.loadAd(adRequest);
When I run the code and I don't specify the test device I usually see a log message in the LOGCAT telling me to add my device id using the addTestDevice api

AdMob banner above Tabs in TabsActivity

I am trying to set the AdMob banner ABOVE the Tabs in a TabsActivity:
public class FundsReportTabsActivity extends TabsActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AdView adView = new AdView(activity, AdSize.BANNER, AD_UNIT_ID);
// Lookup your LinearLayout assuming it’s been given
// the attribute android:id="#+id/mainLayout"
// Add the adView to it
getTabHost().addView(adView, 0, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice("E0154BC3F11C778234254195FA1164F");
adRequest.setTesting(true);
// Initiate a generic request to load it with an ad
adView.loadAd(adRequest);
}
However, the banner does not appear, even though I have this logcat:
09-29 22:23:09.755: INFO/Ads(16158): onReceiveAd()
and don't see any related warning/error.
Does anyone have an idea? Thanks!
Check your logcat for a warning or verbose saying no ads in inventory. Some times when you first sign up or are using admob it takes some time before the ads start appearing. Also make sure you have provided a linear layout for the ad. And if you watch the logcat file during the activity with the ad you will see a content containing items that pertain to the ad. I know i had trouble with admob at first. The same issue you are having. It just took some time.

Categories

Resources