---
title: "JQuery Agenda Google Calendar Sync Example | Mobiscroll"
component: "Agenda"
framework: "jQuery"
library_version: "6.1.5"
url: "https://demo.mobiscroll.com/jquery/agenda/sync-events-google-calendar"
---

# Google Calendar Sync - jQuery Agenda demo

Load and sync Google Calendar events in a JQuery agenda view — authenticate users, toggle calendars on/off, and create events using the plugin.

- [Live demo](https://demo.mobiscroll.com/jquery/agenda/sync-events-google-calendar)
- [View code on GitHub](https://github.com/acidb/mobiscroll-demos-jquery/tree/main/src/demos/eventcalendar/agenda/sync-events-google-calendar)
- This demo in other frameworks: [JavaScript](https://demo.mobiscroll.com/javascript/agenda/sync-events-google-calendar.md), [Angular](https://demo.mobiscroll.com/angular/agenda/sync-events-google-calendar.md), [React](https://demo.mobiscroll.com/react/agenda/sync-events-google-calendar.md), [Vue](https://demo.mobiscroll.com/vue/agenda/sync-events-google-calendar.md)

## Description

The event calendar comes with third party calendar integration support through the [**integration plugin**](https://mobiscroll.com/docs/jquery/eventcalendar/calendar-integrations). This needs to be added to the download package or installed separately from a dedicated NPM package.

It includes everything you need to authenticate your users, get their Google calendars and provides functions for CRUD operations. It takes care of loading the events and converts them to the required format so that they can be displayed on the Mobiscroll calendar separately or mixed with other calendars and events.

Customizing the interaction, event workflows, whether the events are read-only is up to you. In this live demo, newly created events will land in the `primary calendar` of the authenticated user.

This example uses the [**integration plugin**](https://mobiscroll.com/docs/jquery/eventcalendar/calendar-integrations) to connect to and load events from Google Calendar and it is not available in the trial. Give the live demo a try and see how it can be configured.

## What this demo shows

- Shows a monthly agenda view with events listed and grouped by date.
- **Header navigation** The month and year label in the top left opens date navigation. The previous and next arrows and the Today button on the right move between months and jump back to the current date.
- **Signed-out state** No events are loaded by default. The `Sync My Google Calendars` button in the header prompts the user to sign in to Google Calendar.
- **Authentication flow** Clicking the Google sign-in button opens a new browser tab where the user can authenticate with their Google account.
- **Signed-in state** After sign-in, the header button changes to `My Calendars`.
- **Calendar list** The `My Calendars` popup shows multiple calendar switches, such as the user's personal calendar and country-specific holiday calendars.
- **Sign-out** The bottom of the popup includes a `Log out of my account` button for disconnecting the Google Calendar account.
- **Agenda list** The area below the header lists events for the selected month. Events in the agenda are grouped by date.
- **Sticky day headers** As the agenda scrolls vertically, each day stays visible with a sticky date header while its events appear underneath.
- **Events** Events appear as agenda cards with a colored strip on the left and the event title next to it. Start and end times are stacked on the right side of timed events. All-day events show the all-day label on the right.
- **Event interaction** Hovering an event highlights it.
- **Event selection** Clicking an event selects and highlights it.

## Best for

- **Personal calendar scheduling** Building agenda views where users can browse and manage events from their own Google Calendar.
- **Calendar visibility controls** Letting users toggle individual Google calendars on or off inside the same agenda view.
- **Readonly calendar handling** Displaying calendars without write access while preventing edits to those calendar rows.

## API used in this demo

- [`theme`](https://mobiscroll.com/docs/jquery/eventcalendar/api#opt-theme) - option
- [`themeVariant`](https://mobiscroll.com/docs/jquery/eventcalendar/api#opt-themeVariant) - option
- [`setEvents`](https://mobiscroll.com/docs/jquery/eventcalendar/api#method-setEvents) - method
- [`close`](https://mobiscroll.com/docs/jquery/popup/api#method-close) - method
- [`view`](https://mobiscroll.com/docs/jquery/eventcalendar/api#opt-view) - option
- [`exclusiveEndDates`](https://mobiscroll.com/docs/jquery/eventcalendar/api#opt-exclusiveEndDates) - option
- [`onPageLoading`](https://mobiscroll.com/docs/jquery/eventcalendar/api#event-onPageLoading) - event
- [`renderHeader`](https://mobiscroll.com/docs/jquery/eventcalendar/api#renderer-renderHeader) - renderer
- [`anchor`](https://mobiscroll.com/docs/jquery/popup/api#opt-anchor) - option
- [`display`](https://mobiscroll.com/docs/jquery/popup/api#opt-display) - option
- [`scrollLock`](https://mobiscroll.com/docs/jquery/popup/api#opt-scrollLock) - option
- [`showOverlay`](https://mobiscroll.com/docs/jquery/popup/api#opt-showOverlay) - option
- [`touchUi`](https://mobiscroll.com/docs/jquery/popup/api#opt-touchUi) - option
- [`width`](https://mobiscroll.com/docs/jquery/popup/api#opt-width) - option
- [`open`](https://mobiscroll.com/docs/jquery/popup/api#method-open) - method
- [`navigate`](https://mobiscroll.com/docs/jquery/eventcalendar/api#method-navigate) - method

## Code

### JS

```js
mobiscroll.setOptions({
  theme: 'ios',
  themeVariant: 'light'
});

$(function () {
  var googleCalendarSync = mobiscroll.googleCalendarSync;
  var calendarIds = [];
  var myEvents = [];
  var startDate;
  var endDate;
  var loader;
  var timer;

  function isLoading(loading) {
    loader = loader || $('#demo-sync-google-cal-loader');
    loader.css('visibility', loading ? 'visible' : 'hidden');
  }

  function toggleButtons(loggedIn) {
    if (loggedIn) {
      $btnSignIn.hide();
      $btnCalPopup.show();
    } else {
      $btnSignIn.show();
      $btnCalPopup.hide();
    }
  }

  function toggleCalendar(checked, calendarId) {
    if (checked) {
      calendarIds.push(calendarId);
    } else {
      calendarIds = calendarIds.filter(function (id) {
        return id !== calendarId;
      });
    }
    if (calendarIds.length === 0) {
      myEvents = [];
      inst.setEvents(myEvents);
      return;
    }
    isLoading(true);
    googleCalendarSync
      .getEvents(calendarIds, startDate, endDate)
      .then(function (resp) {
        myEvents = resp;
        inst.setEvents(myEvents);
        isLoading(false);
      })
      .catch(function (error) {
        onError(error);
      });
  }

  function onError(resp) {
    mobiscroll.toast({
      message: resp.error ? resp.error : resp.result.error.message,
    });
  }

  function onSignedIn() {
    toggleButtons(true);
    calendarIds = [];
    googleCalendarSync
      .getCalendars()
      .then(function (calendars) {
        var calList = '';

        calendars.sort(function (c) {
          return c.primary ? -1 : 1;
        });

        calendars.forEach(function (c) {
          calList +=
            '<label>' +
            '<input mbsc-switch value="' +
            c.id +
            '" data-label="' +
            c.summary +
            '" type="checkbox" class="mds-google-cal-switch" checked />' +
            '</label>';

          calendarIds.push(c.id);
        });

        $calendarList.html(calList);
        mobiscroll.enhance($calendarList[0]);

        isLoading(true);

        return googleCalendarSync.getEvents(calendarIds, startDate, endDate);
      })
      .then(function (resp) {
        myEvents = resp;
        inst.setEvents(myEvents);
        isLoading(false);
      })
      .catch(onError);
  }

  function onSignedOut() {
    toggleButtons(false);
    calendarIds = [];
    myEvents = [];
    inst.setEvents([]);
    popup.close();
  }

  var inst = $('#demo-sync-google-cal')
    .mobiscroll()
    .eventcalendar({
      view: {
        agenda: { type: 'month' },
      },
      exclusiveEndDates: true,
      onPageLoading: function (args) {
        startDate = args.viewStart;
        endDate = args.viewEnd;
        clearTimeout(timer);
        timer = setTimeout(function () {
          if (googleCalendarSync.isSignedIn()) {
            isLoading(true);
            googleCalendarSync
              .getEvents(calendarIds, startDate, endDate)
              .then(function (resp) {
                myEvents = resp;
                inst.setEvents(myEvents);
                isLoading(false);
              })
              .catch(onError);
          }
        }, 200);
      },
      renderHeader: function () {
        return (
          '<div mbsc-calendar-nav></div>' +
          '<div id="demo-sync-google-cal-loader" class="mds-loader"></div>' +
          '<div class="mbsc-flex mbsc-flex-1-0 mbsc-justify-content-end">' +
          '<button mbsc-button id="demo-sync-google-cal-sign-in">Sync my Google calendars</button>' +
          '<button mbsc-button id="demo-sync-google-cal-popup-btn" style="display:none">My Calendars</button>' +
          '<button mbsc-button id="demo-sync-google-cal-today">Today</button></div>' +
          '</div>' +
          '<button mbsc-calendar-prev></button>' +
          '<button mbsc-calendar-next></button>'
        );
      },
    })
    .mobiscroll('getInst');

  var $btnSignIn = $('#demo-sync-google-cal-sign-in');
  var $btnSignOut = $('#demo-sync-google-cal-sign-out');
  var $btnCalPopup = $('#demo-sync-google-cal-popup-btn');
  var $btnCalToday = $('#demo-sync-google-cal-today');
  var $calendarList = $('#demo-sync-google-cal-list');

  var popup = $('#demo-sync-google-cal-popup')
    .mobiscroll()
    .popup({
      anchor: $btnCalPopup[0],
      contentPadding: false,
      display: 'anchored',
      scrollLock: false,
      showOverlay: false,
      touchUi: false,
      width: 400,
    })
    .mobiscroll('getInst');

  $btnCalPopup.on('click', function () {
    popup.open();
  });

  $btnCalToday.on('click', function () {
    inst.navigate(new Date());
  });

  $btnSignIn.on('click', function () {
    if (!googleCalendarSync.isSignedIn()) {
      googleCalendarSync.signIn().catch(onError);
    }
  });

  $btnSignOut.on('click', function () {
    googleCalendarSync.signOut().catch(onError);
  });

  $calendarList.on('change', '.mds-google-cal-switch', function (ev) {
    toggleCalendar(ev.target.checked, ev.target.value);
  });

  // Init Google Client
  googleCalendarSync.init({
    apiKey: '<YOUR_GOOGLE_API_KEY>',
    clientId: '<YOUR_GOOGLE_CLIENT_ID>',
    onSignedIn: onSignedIn,
    onSignedOut: onSignedOut,
  });
});
```

### HTML

```html
<div id="demo-sync-google-cal"></div>
<div id="demo-sync-google-cal-popup">
  <div class="mbsc-form-group-inset">
    <div class="mbsc-form-group-title">My calendars</div>
    <div id="demo-sync-google-cal-list"></div>
  </div>
  <div class="mbsc-form-group-inset">
    <button mbsc-button id="demo-sync-google-cal-sign-out" class="mbsc-button-block">Sign out of my account</button>
  </div>
</div>
```

### CSS

```css
.mds-loader {
  width: 32px;
  height: 32px;
  border: 4px solid #8c8c8c;
  border-bottom-color: transparent;
  border-radius: 50%;
  display: inline-block;
  box-sizing: border-box;
  animation: mds-loader-rotation 1s linear infinite;
  visibility: hidden;
}

@keyframes mds-loader-rotation {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
```

## Implementation notes

- Install `@mobiscroll/calendar-integration`. At component init call `googleCalendarSync.init({ apiKey: '<YOUR_GOOGLE_API_KEY>', clientId: '<YOUR_GOOGLE_CLIENT_ID>', onSignedIn, onSignedOut })`. Use `view: { agenda: { type: 'month' } }` with `exclusiveEndDates: true`.
- Maintain a `calendarIds` array for the currently enabled calendars. In `onPageLoading` store `args.viewStart`/`args.viewEnd`, debounce 200ms, then if signed in call `googleCalendarSync.getEvents(calendarIds, startDate, endDate)` and pass the result to `data`. For the imperative API call `inst.setEvents()`.
- In `onSignedIn`: call `googleCalendarSync.getCalendars()` to get the user's calendar list; initialise `calendarIds` with all calendar IDs (all enabled by default); call `googleCalendarSync.getEvents()` for the current view range. In `onSignedOut`: clear calendars, IDs, and events.
- Build a custom header via `renderHeader` (Angular: `headerTemplate`, Vue: `header` slot) containing: `CalendarNav`, a CSS spinning loader (visible while loading), then a right-aligned flex block with either a "Sync my Google calendars" Button (before login, calls `googleCalendarSync.signIn()`) or a "My Calendars" Button (after login, opens the calendar-list Popup) plus a "Today" Button. End with `CalendarPrev` and `CalendarNext`. The "Today" button updates `selectedDate` to today; JS/jQuery: call `inst.navigate(new Date())`.
- Show the calendar list in a `Popup` anchored to the "My Calendars" button: `display: 'anchored'`, `contentPadding: false`, `scrollLock: false`, `showOverlay: false`, `touchUi: false`, `width: 400`. Inside, render each calendar as a `Switch` - toggling one adds or removes its ID from `calendarIds` and re-fetches events via `googleCalendarSync.getEvents()`. Include a "Sign out" Button that calls `googleCalendarSync.signOut()`. Show API errors in a `Toast`.

## Run it in your project

Running a demo locally requires a Mobiscroll trial or a license; setup is per framework at https://mobiscroll.com/docs/jquery/getting-started/installation

## Related demos

- Next: [Outlook Calendar Sync](https://demo.mobiscroll.com/jquery/agenda/sync-events-outlook-calendar.md) - Use the JQuery event calendar integration plugin to authenticate users, load their Outlook calendars, and display events in an agenda view.
