Posts tagged with google-ads-api

I have included the needed script tag in the global template.html file which contains all dynamic pages for my SPA (written in Sapper/Svelte).

www.ensueco.com

<script data-ad-client="ca-pub-XXXXXXXXXXXXXXXX" async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> 

​If I load the index page, auto ads load perfectly, but when I click a link navigating to another page/article, client-side routing will change the content in the content window, but won't recreate new autogenerated ads.

If I open a given article/subpage as initial page load, it also loads ads from auto ads perfectly, but clicking "home" in main navigation going back to the homepage, doesn't load new ads in the content window.

Q: How do I ask the adsbygoogle script to execute again on client-side routing to fill in auto ads on all pages?

I currently have a script that subscribes to route change events and forward these informations to Google Analytics:

page.subscribe(({ path, params, query }) => { gtag("config", "GOOGLE_ANALYTICS", { page_path: path }); }); 

But not sure if I could do something similar in this to ask adsbygoogle to execute again? (below code has been tested and doesn't work)

page.subscribe(({ path, params, query }) => { // (window.adsbygoogle = []).push({ // google_ad_client: "ca-pub-XXXXXXXXXXXXX", // enable_page_level_ads: true, // tag_partner: "site_kit" // }); gtag("config", "GOOGLE_ANALYTICS", { page_path: path }); }); 

Above code at testing gives me this:

I get this error on route change then:

Uncaught O {message: "adsbygoogle.push() error: Only one 'enable_page_level_ads' allowed per page.", name: "TagError", pbr: true, stack: "TagError: adsbygoogle.push() error: Only one 'enab…esyndication.com/pagead/js/adsbygoogle.js:58:409)"} 

And if I remove the enable_page_level_ads I get this:

Uncaught  O {message: "adsbygoogle.push() error: All ins elements in the … with class=adsbygoogle already have ads in them.", name: "TagError", pbr: true, stack: "TagError: adsbygoogle.push() error: All ins elemen…js/adsbygoogle.js:185:25)↵    at <anonymous>:1:38"} 

So basically, I need to execute some method that creates new tags (based on auto ads) so I can execute this method and populate all the new ins elements. I think.

Thanks


ADDITIONAL THOUGHTS

From what I understand, the script is loaded post SSR (server-side rendering), why the content, HTML, CSS, and JS from my site already is loaded once the adsbygoogle script is executed.

From a development POW, I would expect it to be possible to empty the adsbygoogle array and re-initialize the ads by google script in order for the JS, to crawl the now dynamically loaded content and based on the new window.pushed URI (window.history.pushState()), and place new ads as it would have done anyway, should it have been loaded after this particular page's SSR.

My AdMob banner is not showing in my android application. After seeing older questions about the same problem none helped because they refer to a previous version of android studio. Now some things have become deprecated.

My NewRecord.java file contatins the following code:

package com.example.anxietycalendar; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import com.akaita.android.circularseekbar.CircularSeekBar; import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.InterstitialAd; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.initialization.InitializationStatus; import com.google.android.gms.ads.initialization.OnInitializationCompleteListener; import java.text.DateFormat; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; public class NewRecord extends AppCompatActivity {     DatabaseHelper myDB;     private Button Submit;     public float pr;     private AdView mAdView;     private InterstitialAd mInterstitialAd;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_new_record);         MobileAds.initialize(this, new OnInitializationCompleteListener() {             @Override             public void onInitializationComplete(InitializationStatus initializationStatus) {             }         });         mAdView = findViewById(R.id.adView);         AdRequest adRequest = new AdRequest.Builder().build();         mAdView.loadAd(adRequest);         mAdView.setAdListener(new AdListener() {                                   @Override                                   public void onAdFailedToLoad(int errorCode) {                                       String onAdFailedToLoad = null;                                       Log.d(onAdFailedToLoad, "This is why: " + errorCode);                                   }         });         myDB=new DatabaseHelper(this);         Submit= (Button) findViewById(R.id.submit);         CircularSeekBar seekBar = (CircularSeekBar) findViewById(R.id.seekbar);         seekBar.setProgressTextFormat(new DecimalFormat("###,###,###,##0.00"));         seekBar.setProgress(0);         seekBar.setRingColor(Color.GREEN);         seekBar.setOnCircularSeekBarChangeListener(new CircularSeekBar.OnCircularSeekBarChangeListener() {             @Override             public void onProgressChanged(CircularSeekBar seekBar, float progress, boolean fromUser) {                 if (progress < 5) {                     seekBar.setRingColor(Color.GREEN);                     pr = progress;                 } else if (progress < 8) {                     seekBar.setRingColor(Color.YELLOW);                     pr = progress;                 } else {                     seekBar.setRingColor(Color.RED);                     pr = progress;                 }             }             @Override             public void onStartTrackingTouch(CircularSeekBar seekBar) {             }             @Override             public void onStopTrackingTouch(CircularSeekBar seekBar) {             }         });         Submit.setOnClickListener(new View.OnClickListener() {             @Override             public void onClick(View v) {                 myDB.insertData(Math.round(pr));             }         });     } } 

While the activity_new_record.xml contains the following code:

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:background="@drawable/background"     tools:context=".NewRecord">     <TextView         android:id="@+id/perctext"         android:layout_width="281dp"         android:layout_height="36dp"         android:layout_marginTop="36dp"         android:background="@drawable/bg_calen2"         android:ems="10"         android:gravity="center"         android:inputType="textPersonName"         android:text="How much stress do you feel?"         android:textColor="@color/white"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintHorizontal_bias="0.43"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toTopOf="parent" />     <com.akaita.android.circularseekbar.CircularSeekBar         android:id="@+id/seekbar"         android:layout_width="300dp"         android:layout_height="224dp"         android:layout_marginTop="52dp"         app:enabled="true"         app:innerCircleColor="#e0f7e9"         app:layout_constraintHorizontal_bias="0.495"         app:layout_constraintLeft_toLeftOf="parent"         app:layout_constraintRight_toRightOf="parent"         app:layout_constraintTop_toBottomOf="@+id/perctext"         app:max="10"         app:min="0"         app:progress="1"         app:progressTextColor="@android:color/black"         app:progressTextSize="26"         app:ringColor="@color/colorAccent"         app:ringWidth="0.5"         app:showIndicator="true"         app:showInnerCircle="true"         app:showProgressText="true"         app:speedMultiplier="2" />     <Button         android:id="@+id/submit"         style="@style/BlueButton"         android:layout_width="271dp"         android:layout_height="31dp"         android:text="Submit Record"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintEnd_toEndOf="parent"         app:layout_constraintStart_toStartOf="parent"         app:layout_constraintTop_toBottomOf="@+id/seekbar"         app:layout_constraintVertical_bias="0.168" />     <com.google.android.gms.ads.AdView         android:id="@+id/adView"         xmlns:ads="http://schemas.android.com/apk/res-auto"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_alignParentBottom="true"         android:layout_centerHorizontal="true"         ads:adSize="BANNER"         ads:adUnitId="ca-app-pub-6417467530528623/7094248892"         ads:layout_constraintBottom_toBottomOf="parent"         ads:layout_constraintEnd_toEndOf="parent"         ads:layout_constraintHorizontal_bias="0.615"         ads:layout_constraintStart_toStartOf="parent"         ads:layout_constraintTop_toBottomOf="@+id/submit"         ads:layout_constraintVertical_bias="0.198" /> </androidx.constraintlayout.widget.ConstraintLayout> 

This is the activity where I am trying to add the banner app. I have tried to use the test ad unit Id's but it is still failing.

I would also like to add this part of the code of the AndroidManifest.xml:

<application         android:allowBackup="true"         android:icon="@mipmap/ic_launcher"         android:label="Anxiety Calendar"         android:roundIcon="@mipmap/ic_launcher_round"         android:supportsRtl="true"         android:theme="@style/AppTheme"         tools:replace="android:label">         <activity android:name=".Statistics"></activity>         <!-- Include the AdActivity configChanges and theme. -->         <activity             android:name="com.google.android.gms.ads.AdActivity"             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"             android:theme="@android:style/Theme.Translucent" /> <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->         <meta-data             android:name="com.google.android.gms.ads.APPLICATION_ID"             android:value="ca-app-pub-6417467530528623~3872355031" /> 

How do I fix this? I have been trying for days... I am using a Galaxy Nexus API 22 simulator.

I am on a new App and in that I want to show some ads on the bottom. The AdView however is height 0dp at app startup and inflates only when the ads are loaded and then resizes the fragment and moves its content and children, which I want to avoid obviously.

My app is build like following:

In my MainActivity view I have a ConstraintLayout which has the Toolbar, the fragment container and the AdView as children and it's arranged like in the screenshot above. So the container is between the AdView and Toolbar which is causing the problem.

Of course I can make the fragment container align to the bottom of its parent but then the ads are shown above the containers content which can cause problems on smaller screens.

Is there any way I can somehow "reserve" the AdViews size upon app startup or something like this?

I tried with google ads api npm with the query

SELECT ad_group_ad.ad.name, ad_group_ad.ad.type , ad_group_ad.ad.app_ad.headlines,ad_group_ad.ad.app_engagement_ad.headlines,ad_group_ad.ad.text_ad.headline FROM ad_group_ad

Response

{ ad: { resource_name: 'customers/xxxxx/ads/xxxxxx', type: 3 }, resource_name: 'customers/xxxxx/adGroupAds/xxxx~xxxxxx }

But response not containing ad name. Ad Id and resource name is there.

How can I get the ad name?