Пример #1
0
  @Override
  public void onStart() {
    super.onStart();

    EasyTracker.getInstance().activityStart(this);

    final GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
    mTracker = analytics.getTracker(getString(R.string.ga_trackingId));
    mTracker.sendEvent("activity_phone", "start", "phone", 0l);

    GAServiceManager.getInstance().dispatch();
  }
  @Override
  public boolean execute(String action, JSONArray args, CallbackContext callback) {
    if (action.equals("initGTM")) {
      try {
        // Set the dispatch interval
        GAServiceManager.getInstance().setLocalDispatchPeriod(args.getInt(1));

        TagManager tagManager =
            TagManager.getInstance(this.cordova.getActivity().getApplicationContext());
        ContainerOpener.openContainer(
            tagManager, // TagManager instance.
            args.getString(0), // Tag Manager Container ID.
            OpenType
                .PREFER_NON_DEFAULT, // Prefer not to get the default container, but stale is OK.
            null, // Time to wait for saved container to load (ms). Default is 2000ms.
            new ContainerOpener.Notifier() { // Called when container loads.
              @Override
              public void containerAvailable(Container container) {
                // Handle assignment in callback to avoid blocking main thread.
                mContainer = container;
                inited = true;
              }
            });
        callback.success(
            "initGTM - id = " + args.getString(0) + "; interval = " + args.getInt(1) + " seconds");
        return true;
      } catch (final Exception e) {
        callback.error(e.getMessage());
      }
    } else if (action.equals("exitGTM")) {
      try {
        inited = false;
        callback.success("exitGTM");
        return true;
      } catch (final Exception e) {
        callback.error(e.getMessage());
      }
    } else if (action.equals("trackEvent")) {
      if (inited) {
        try {
          DataLayer dataLayer =
              TagManager.getInstance(this.cordova.getActivity().getApplicationContext())
                  .getDataLayer();
          int value = 0;
          try {
            value = args.getInt(3);
          } catch (Exception e) {
          }
          dataLayer.push(
              DataLayer.mapOf(
                  "event",
                  "interaction",
                  "target",
                  args.getString(0),
                  "action",
                  args.getString(1),
                  "target-properties",
                  args.getString(2),
                  "value",
                  value));
          callback.success(
              "trackEvent - category = "
                  + args.getString(0)
                  + "; action = "
                  + args.getString(1)
                  + "; label = "
                  + args.getString(2)
                  + "; value = "
                  + value);
          return true;
        } catch (final Exception e) {
          callback.error(e.getMessage());
        }
      } else {
        callback.error("trackEvent failed - not initialized");
      }
    } else if (action.equals("trackPage")) {
      if (inited) {
        try {
          DataLayer dataLayer =
              TagManager.getInstance(this.cordova.getActivity().getApplicationContext())
                  .getDataLayer();
          dataLayer.push(DataLayer.mapOf("event", "content-view", "content-name", args.get(0)));
          callback.success("trackPage - url = " + args.getString(0));
          return true;
        } catch (final Exception e) {
          callback.error(e.getMessage());
        }
      } else {
        callback.error("trackPage failed - not initialized");
      }
    } else if (action.equals("dispatch")) {
      if (inited) {
        try {
          GAServiceManager.getInstance().dispatchLocalHits();
          callback.success("dispatch sent");
          return true;
        } catch (final Exception e) {
          callback.error(e.getMessage());
        }
      } else {
        callback.error("dispatch failed - not initialized");
      }
    }
    return false;
  }